Angular ngOptions 按对象键跟踪
Angular ngOptions track by object keys
我想用 ngOptions 创建一个 select 元素——目前一切顺利:
<select ng-options="currency for (code, currency) in currencies track by code"
ng-model="something.currency"></select>
现在我想通过对象键跟踪选项(因此 ng-model
的值应该是键而不是对象的值)。这可以通过一些简单的方式实现吗?如何实现?
是您要找的吗?
var myApp = angular.module('myApp',[]);
myApp.controller('GreetingController', ['$scope', function($scope) {
$scope.currencies_old = [
{code: "USD", curr: "$"},
{code: "IND", curr: "INR"}
];
$scope.currencies = {"USD": "USDollars", "EUR": "Euro"};
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="GreetingController">
<select ng-model="item" ng-options="key as value for (key , value) in currencies"></select>
Code: {{item}}
<br/> <br/>
<select ng-options="item as item.curr for item in currencies_old track by item.code" ng-model="item_old"></select>
Code: {{item_old.code}}
</div>
我想用 ngOptions 创建一个 select 元素——目前一切顺利:
<select ng-options="currency for (code, currency) in currencies track by code"
ng-model="something.currency"></select>
现在我想通过对象键跟踪选项(因此 ng-model
的值应该是键而不是对象的值)。这可以通过一些简单的方式实现吗?如何实现?
是您要找的吗?
var myApp = angular.module('myApp',[]);
myApp.controller('GreetingController', ['$scope', function($scope) {
$scope.currencies_old = [
{code: "USD", curr: "$"},
{code: "IND", curr: "INR"}
];
$scope.currencies = {"USD": "USDollars", "EUR": "Euro"};
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="GreetingController">
<select ng-model="item" ng-options="key as value for (key , value) in currencies"></select>
Code: {{item}}
<br/> <br/>
<select ng-options="item as item.curr for item in currencies_old track by item.code" ng-model="item_old"></select>
Code: {{item_old.code}}
</div>