我想知道AngularJS ng-options - 是否可以在下拉列表中使用2个不同的变量作为名称?
采用这个简化的示例 - 我希望下拉列表中包含Country - City。我知道这是简单的,但这可能与组合选择下拉列表之外的变量有关吗?
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
</head>
<body ng-app="myapp">
<div ng-controller="HelloController" >
<select ng-model="blah" ng-options="city.Id as city.CityName for city in cities"></select>
</div>
<script>
angular.module("myapp", [])
.controller("HelloController", function($scope) {
$scope.cities = [
{Id: '000001', Country:'USA', CityName: 'Chicago'},
{Id: '000002', Country:'Ireland', CityName: 'Dublin'},
{Id: '000003', Country:'Spain', CityName: 'Barcelona'},
{Id: '000004', Country:'USA', CityName: 'Boston'},
];
} );
</script>
</body>
</html>
答案 0 :(得分:2)
你应该能够这样做:
ng-options="city.Id as (city.Country + ' - ' + city.CityName) for city in cities"