我编写了如下代码
<select ng-model="selectedseasId" ng-options="seas.seasInternalId as seas.seasName for seas in seass track by seas.seasInternalId"> </select>
$scope.selectedseasId = 3;
我有像
这样的数据var seass = [ {
"seasInternalId" : 4,
"seasName": "My Second seas"
},
{
"seasInternalId" : 3,
"seasName": "My Second seas"
}
];
在选择框中未选择任何值。我的代码有什么问题吗?
答案 0 :(得分:4)
ng-options跟踪并选择不兼容。见link
当然,你不需要ID的轨道,因为我认为seasInternalId是唯一的,所以seas.seasInternalId as seas.seasName for seas in seass
应该足够并且有效。
答案 1 :(得分:3)
您不能在ng-options表达式中使用track by和select。来自文档:
请勿在同一表达式中使用选择和跟踪。他们不是 旨在合作。
通过表达式的一部分删除曲目,它将起作用。
答案 2 :(得分:0)
您可以使用ng-repeat代替ng-option
<!DOCTYPE html>
<html>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<select>
<option ng-repeat="sea in seass">{{sea.seasName}}
</select>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope,$http)
{
$scope.seass = [
{
"seasInternalId" : 4,
"seasName": "My Second seas 4"
},
{
"seasInternalId" : 3,
"seasName": "My Second seas 3"
}
];
$scope.seasInternalId = 4;
});
</script>
</body>
</html>