Plunkr:http://plnkr.co/edit/RgNcSg?p=preview
我有一个由对象数组填充的“位置”下拉列表。如果我设置了我在控制器中执行的对象值,则在下拉列表中未选中它,为什么不呢?您可以通过html输出看到绑定有效并且值设置正确,但无法从控制器设置下拉框。
控制器:
var myapp = angular.module('myapp', []);
myapp.controller('ctrl', function($scope) {
$scope.locationOptions = [
{id: 1, name: "option 1"},
{id: 2, name: "option 2"}];
$scope.options = {
location: { id:1, name: "option 1"}
}
});
模板:
<div class="form-group">
<label for="location" class="control-label col-md-2">Location</label>
<div class="col-md-3">
<select id="location"
class="form-control"
ng-options="loc.name for loc in locationOptions"
ng-disabled="loadingLocations"
ng-model="options.location">
</select>
</div>
</div>
LocationID: {{options.location.id}}
Location: {{options.location.name}}
答案 0 :(得分:1)
这是因为angularjs使用了对链接的引用,试着这样做:
$scope.options = $scope.locationOptions[0]