angular ng-model 绑定的值不更新

angular ng-model 绑定的值不更新

代码主要部分如下

// html代码
<div class="form-group">
  <label>Slot : </label>
  <select class="form-control" ng-model="selectedSlot" ng-options="slot for slot in slotlist"></select>
</div>

//javascript代码
$scope.slotlist = ['Flash Ads', 'Native Ads', 'Lock Ads'];
$scope.selectedSlot = $scope.slotlist[0];

如代码所示,为什么我改变下拉框的值时,$scope.selectedSlot 并没有动态改变?
是不是因为$scope.selectedSlot是数组的一个值,相当于常量?

求详细解答

阅读 7.6k
3 个回答

selectedSlot 换成对象格式:
selectedSlot.someAttr

我前两天在项目中也遇到了这个问题。
我的问题是使用ng-if指令产生了新的scope(继承了controller的scope)。angularjs中的其他指令也存在这样的问题。selectedSlot将这个属性封装成对象的一个属性即可。$scope.selectedSlotObj = {selectedSlot: '...'}的形式即可

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题