我可以访问在ng-options对象的索引



我使用ng-options来显示对象数组中的数据

index . html

<select ng-model="object_choose" 
ng-options="values.id as values.label for values in array_object">
</select>
<button ng-click="get_my_array()">Get Array</button>

controller.js

app.controller('Ctrl', function ($scope) {
$scope.array_object = [
    {"id": "a", "label": "A", "my_array": [1, 2, 3, 4]},
    {"id": "b", "label": "B", "my_array": [1, 4, 8]},
    {"id": "c", "label": "C", "my_array": [2,6]}
];
$scope.get_my_array = (function() {
      console.log($scope.object_choose); //It work and show "id" a , b , c
      console.log($scope.object_choose.my_array); //It not work
  });  
});

最简单的方法是将实际值存储在您的模型中:

ng-options="values as values.label for values in array_object"

你可以这样访问它:

console.log($scope.object_choose.id);       // a , b , c
console.log($scope.object_choose.my_array); // the array

技巧在于理解编写表达式来操作数据存储和显示的多种方式。手册里有你需要的所有信息。

最新更新