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

后端 未结 27 1195
感动是毒
感动是毒 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:43

    ngOptions directive:

    $scope.items = [{name: 'a', age: 20},{ name: 'b', age: 30},{ name: 'c', age: 40}];
    
    • Case-1) label for value in array:

      selected item is : {{selectedItem}}

      age of selected item is : {{selectedItem.age}}

    Output Explanation (Assume 1st item selected):

    selectedItem = {name: 'a', age: 20} // [by default, selected item is equal to the value item]

    selectedItem.age = 20

    • Case-2) select as label for value in array:

      selected item is : {{selectedItem}}

    Output Explanation (Assume 1st item selected): selectedItem = 20 // [select part is item.age]

提交回复
热议问题