Angularjs 1.5.5
我有一个简单的Select
,效果很好:
<select ng-model="selectedPart">
<option ng-repeat="optionPart in parts" value="{{optionPart.Id}}">{{optionPart.Title}}</option>
</select>
当我将id设置为字符串时,Select
会正确呈现:
$scope.selectedPart = "1";
当我从对象将其设置为Int时,不会进行任何更改。
$scope.selectedPart = $scope.parts[0].Id;
为什么Int
不会更新Select
而string
会更新,以及如何让它工作?
答案 0 :(得分:2)
尝试这样做
<select ng-model="selectedPartId"
ng-options="part.Id as part.Title for part in parts">
</select>
请注意,我们已更改模型,以反映您选择id
而不是parts
条目。
如果您确实要绑定对象,请尝试使用
<select ng-model="selectedPart"
ng-options="part.Title for part in parts track by part.Id">
</select>
然后您可以通过$scope.selectedPart = $scope.parts[0];
答案 1 :(得分:0)
有两种方法可以做到:
1. $scope.parts = [{
id: 2,
Title: 'XX'
}, {
id: 5,
Title: 'XXXX'
}, {
id: 8,
Title: 'XXVVV'
}];
$scope.selectedPart = $scope.parts[1];
<select ng-options="part as part.Title for part in parts track by part.id" ng-model="selectedPart"></select>
2. $scope.parts = [{
id: 2,
Title: 'XX'
}, {
id: 5,
Title: 'XXXX'
}, {
id: 8,
Title: 'XXVVV'
}];
$scope.selectedPart = $scope.parts[1].id;
<select ng-options="part.id as part.Title for part in parts" ng-model="selectedPart"></select>