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

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

    Here is how I solve this problem in a legacy application:

    In HTML:

    ng-options="kitType.name for kitType in vm.kitTypes track by kitType.id" ng-model="vm.itemTypeId"
    

    In JavaScript:

    vm.kitTypes = [
        {"id": "1", "name": "Virtual"},
        {"id": "2", "name": "Physical"},
        {"id": "3", "name": "Hybrid"}
    ];
    

    ...

    vm.itemTypeId = vm.kitTypes.filter(function(value, index, array){
        return value.id === (vm.itemTypeId || 1);
    })[0];
    

    My HTML displays the option value properly.

提交回复
热议问题