ng-options:如何访问 object 中没有键或值索引的位置(索引)
ng-options: How to access to position (index) in an object which has no index in key or value
我有一个 JSON 对象。如下:
[
{"Title":"x", "Type":"y", "Desc":"First Description"},
{"Title":"r", "Type":"q", "Desc":"Second Description"}
]
我想要 drop-down 在这个对象中包含标题(使用 ng-options
)。然后,我想使用 object 中所选标题的索引。 (假设 object 将用作数组(称为 myArray),正如 user194715 回答的那样 here)
例如:
var type = myArray[selectedIndex].Type;
var description= myArray[selectedIndex].Desc;
在这种情况下如何访问索引?
这样试试
var app = angular.module("app", []);
app.controller('mainCtrl', function($scope){
$scope.myArray =
[
{"Title":"x", "Type":"y", "Desc":"First Description"},
{"Title":"r", "Type":"q", "Desc":"Second Description"}
];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="mainCtrl">
<select ng-model="select" ng-options="key as value.Title for (key,value) in myArray">
</select>
<span>{{myArray[select].Type}}</span>
<span>{{myArray[select].Desc}}</span>
</div>
我有一个 JSON 对象。如下:
[
{"Title":"x", "Type":"y", "Desc":"First Description"},
{"Title":"r", "Type":"q", "Desc":"Second Description"}
]
我想要 drop-down 在这个对象中包含标题(使用 ng-options
)。然后,我想使用 object 中所选标题的索引。 (假设 object 将用作数组(称为 myArray),正如 user194715 回答的那样 here)
例如:
var type = myArray[selectedIndex].Type;
var description= myArray[selectedIndex].Desc;
在这种情况下如何访问索引?
这样试试
var app = angular.module("app", []);
app.controller('mainCtrl', function($scope){
$scope.myArray =
[
{"Title":"x", "Type":"y", "Desc":"First Description"},
{"Title":"r", "Type":"q", "Desc":"Second Description"}
];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="mainCtrl">
<select ng-model="select" ng-options="key as value.Title for (key,value) in myArray">
</select>
<span>{{myArray[select].Type}}</span>
<span>{{myArray[select].Desc}}</span>
</div>