我在“视图文件”中有这个
<select class="form-control" id="selection" ng-model="currentSelected"
ng-options="selection.id as selection.name for selection in selections track by selection.id"></select>
这在我的控制器中
$scope.currentSelected = 1;
$scope.selections = [
{
id: 1,
name: 'Name #1'
},
{
id: 2,
name: 'Name #2'
}
];
但是,选择选项是空白的,我希望它在此示例中为“名称#1”。我在这里缺少什么?
答案 0 :(得分:0)
跟踪数组运行良好。如果您希望它是id,那么您应该使用selection.id as selection.name for selection in selections
删除track by selection.id
,它应该可以正常工作。
<强>样本强>
var app = angular.module('todoApp', []);
app.controller("dobController", ["$scope",
function($scope) {
$scope.currentSelected = 1;
$scope.selections = [{
id: 1,
name: 'Name #1'
}, {
id: 2,
name: 'Name #2'
}];
}
]);
&#13;
<!DOCTYPE html>
<html ng-app="todoApp">
<head>
<title>To Do List</title>
<link href="skeleton.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
<script src="MainViewController.js"></script>
</head>
<body ng-controller="dobController">
<select class="form-control" id="selection" ng-model="currentSelected" ng-options="selection.id as selection.name for selection in selections"></select>
</body>
</html>
&#13;