ng-options doesn't work well when using objects
http://jsfiddle.net/ADukg/658/
The example uses ng-options to provide 3 different objects which can be selected. All works until you switch to the next section then comes back. As the implementation uses object comparison, Angular can no longer match the current selected with the datasource.
Conceptually, ng-options should store the 3 selection objects (somewhere) and their lifespan is tied to the "data" object's scope, and not to the ng-options's proxyOptions lifespan.
Also, the comparison should be on a key, and not on the object, in this example, the key is proxyType, there currently is no way to provide the key field to ng-option while still using the object as the value.
One more issue, please checkout http://jsfiddle.net/YDdJf/4 Doesn't use city.cp as option value.
It has always painful to developers(newbies) while working with ng-options, like we get empty/blank selected value in select tag and then we try many of the things like to hide empty space and show the first value as selected, but unfortunately its not selected. :( Especially when we are dealing with JSON object in ng-options, it becomes much tedious. Here I have done some exercises on that.
Objective: Iterate array of JSON objects through ng-option and set selected first element.
Data: someNames = [{"id":"1","someName":"xyz"}, {"id":"2","someName":"abc"}]
In select tag I had to show xyz and abc, where xyz must be selected without much efforts.
HTML:
<select class="form-control" name="test" style="width:160px" ng-options="name.someName for name in someNames" ng-model="testModel.test" ng-selected = "testModel.test = testModel.test || someNames[0]">
</select>
By above code sample, you might get out of this exaggeration.
track by is indeed the way to solve this: http://plnkr.co/edit/FIQ1eORODFibHon12rYz?p=preview
Track by does NOT solve it. Track by can only be used by arrays. The OP is reporting issues with objects.
Converting objects to arrays to "solve" the problem with objects is NOT a solution.
@dailytabs, similarly, converting arrays to objects just so your form data works smoother with id's from the backend is a pretty crappy solution too.
Found the answer using resources http://embed.plnkr.co/ZXo8n0jE8r3LsgdhR0QP/preview
Why does AngularJS include an empty option in select? and my default value is empty ? blow is my code: .directive('gettheme',function( ){ return { restrict:"AE", template:"", link:function(scope, element, attrs){ scope.theme=['macarons', 'infographic']; } } })
You dont have to write a whole directive for it. If you use ng-options angular usually make empty first option, but if you dont want it, you have to define ng-model in your js file like that: $scope.option = ['banana','apple'];$scope.selectedOption = 'banana';
Best Regards.
2017-05-05 6:56 GMT+02:00 fionagithub [email protected]:
Why does AngularJS include an empty option in select? and my default value is empty ? blow is my code: .directive('gettheme',function( ){ return { restrict:"AE", template:"", link:function(scope, element, attrs){ scope.theme=['macarons', 'infographic']; } } })
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/angular/angular.js/issues/1302#issuecomment-299375264, or mute the thread https://github.com/notifications/unsubscribe-auth/AND858Wc3kDgpGkmYTYqBAKCmghWE7MCks5r2qwGgaJpZM4AIH0C .
For anyone still experiencing this, the track by was breaking the ngOptions. Removing it caused the ngModel binding to work as expected.
Below is my component's code:
<select class="form-control"
id="{{::$ctrl.elementId}}"
data-ng-model="$ctrl.selectedKey"
data-ng-options="item.key as item.label for (key, item) in $ctrl.items">
<option value="" ng-if="::$ctrl.placeholder">{{::$ctrl.placeholder}}</option>
</select>