我是 AngularJS 新手,并尝试在选择框上设置默认值。我有一个要在选择框中列出的对象列表。这工作正常,并且与选择框关联的模型的任何绑定(bind)都工作正常。但是,一旦我设置了默认值,我的所有绑定(bind)就会由于某种原因不想更新。
Controller :
app.controller('ctrl', [ '$scope', function ctrl($scope){
api.get('dealerships', function(err, dealerships){
$scope.dealerships = dealerships;
$scope.$apply();
});
});
HTML:
<select ng-model="dealership" ng-options="d.name for d in dealerships"></select>
<span class="dealership-name">{{dealership.name}}</span>
这工作得很好,我可以在经销商之间切换,并且 dealership.name 绑定(bind)也可以很好地更新。但是,一旦我设置了这样的默认值:
app.controller('ctrl', [ '$scope', function ctrl($scope){
api.get('dealerships', function(err, dealerships){
$scope.dealerships = dealerships;
$scope.dealership = $scope.dealerships[0];
$scope.$apply();
});
});
我与经销商的所有绑定(bind)保持不变,它们仍保留在经销商[0]。
用图像可能更容易理解:
有人有任何线索吗?非常感谢任何帮助。
最佳答案
问题已解决。我有两个 ng-controller="ctrl"属性。导航栏上有一个,我的选择框就在那里。还有一个关于主要内容,即发生绑定(bind)的地方。
关于javascript - 无法在 AngularJS 选择框上设置默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19450658/