I have a <select>
element that uses ng-options
to generate the select list and hopefully set the selected to the value of the ng-model
. Here is my html:
<td style="text-align:center">
<ng-form name="IsTobaccoForm">
<select name="Input" required ng-model="dep.IsTobacco" ng-options="item.value as item.text for item in yesNoList"></select>{{dep.IsTobacco}}
<span class="alert-error" ng-show="IsTobaccoForm.Input.$error.required"><strong>*Required</strong></span>
</ng-form>
</td>
My controller contains the following code:
$scope.yesNoList = [{ value: true, text: 'Y' }, { value: false, text: 'N'}];
If the IsTobacco
value of the item is true, the value is set properly and everything is fine. However, if it is false, the required error appears, even though {{dep.IsTobacco}}
renders as false. The really weird part is that if I change the selected item in the list to true, it works fine, however if I set it to false, a new blank item is added to the list and that blank item is selected.
If it seems like the issues do not lie in the lines posted above, I could gladly post more code.
Thanks!
EDIT: I also used chrome debugger and saw that when the data was being initiated in Angular, the value of IsTobacco
was equal to false
.
Edit 2: If anyone viewing this is new to AngularJS, I would definitely recommend reading these 2 articles: part 1 & part 2. They are an overview of AngularJS forms.