How do I set the value property in AngularJS' ng-options?

后端 未结 27 1166
感动是毒
感动是毒 2020-11-22 08:17

Here is what seems to be bothering a lot of people (including me).

When using the ng-options directive in AngularJS to fill in the options for a &

27条回答
  •  孤独总比滥情好
    2020-11-22 08:59

    It is always painful for developers to with ng-options. For example: Getting an empty/blank selected value in the select tag. Especially when dealing with JSON objects in ng-options, it becomes more 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 the select tag I had to show xyz and abc, where xyz must be selected without much effort.

    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.

    Another reference:

提交回复
热议问题