首页 > angular ng-model 绑定的值不更新

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是数组的一个值,相当于常量?

求详细解答


把你的代码拷过去运行了一下,是改变的啊

https://jsfiddle.net/flybywind/0xmzj9hx/


selectedSlot 换成对象格式:
selectedSlot.someAttr


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

【热门文章】
【热门文章】