通过不处理 select 更改值未得到 selected 进行跟踪
track by not working on select change value not getting selected
我写了如下代码
<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"
}
];
在 select 框中没有值被 selected。我的代码有什么问题吗?
您可以使用 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>
您不能像在 ng-options 表达式中那样使用 track by 和 select。来自文档:
Do not use select as and track by in the same expression. They are not
designed to work together.
删除部分表达式的轨道,它会起作用。
ng-options track by 和 select as 不兼容。看到这个 link
当然你不需要按 ID 跟踪,因为我认为 seasInternalId 是唯一的,所以 seas.seasInternalId as seas.seasName for seas in seass
应该足够了。
我写了如下代码
<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"
}
];
在 select 框中没有值被 selected。我的代码有什么问题吗?
您可以使用 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>
您不能像在 ng-options 表达式中那样使用 track by 和 select。来自文档:
Do not use select as and track by in the same expression. They are not designed to work together.
删除部分表达式的轨道,它会起作用。
ng-options track by 和 select as 不兼容。看到这个 link
当然你不需要按 ID 跟踪,因为我认为 seasInternalId 是唯一的,所以 seas.seasInternalId as seas.seasName for seas in seass
应该足够了。