Angular js,$ scope更改未反映在视图中

问题描述 投票:0回答:2

视图部分

 <div class="col-sm-8" data-ng-init="init_enable_disable()">
     <select class="form-control" style="margin-top: 5px;" data-ng-model="schedule.scheduleType" data-ng-change="enable_disableDiv(schedule.scheduleType);">
         <option ng-selected="{{type == defaultSelectedType}}" data-ng-repeat="type in schduleType">{{type}}
         </option>
     </select>
</div>

ng-repeat模型

 $scope.schduleType = ['Recurring', 'One time'];

我正在更新ng-model,但它没有反映在视野中

 if (editRecord.freq_type === 1) 
 {
     alert("one time");
     $scope.schedule.scheduleType = 'One time';
 }

出了什么问题请建议我。

javascript angularjs
2个回答
1
投票

当你使用ng-model并从控制器分配值时,不需要在DOM中使用ng-selected。并且最好从控制器初始化。

可以尝试一下

在你的控制器中:

  $scope.schduleType = ['Recurring', 'One time'];
  $scope.schedule = {};
  $scope.schedule.scheduleType = $scope.schduleType[0]; // initial select

  $scope.enable_disableDiv = function(){
    console.log($scope.schedule.scheduleType);
  }

在HTML中:

<select class="form-control" data-ng-model="schedule.scheduleType" data-ng-change="enable_disableDiv(schedule.scheduleType)">
    <option ng-repeat="type in schduleType" value="{{type}}">{{type}}</option>
</select>

N.B:如果你想从你的控制器改变选项值,你应该使用$scope.schduleType而不是someValue进行引用,因为从控制器分配值然后不参考schduleType。喜欢根据条件改变然后使用如下:

if(editRecord.freq_type === 1) {
   $scope.schedule.scheduleType = $scope.schduleType[1];// not "one Time"
}

0
投票

你可以试试

`$scope.$applyAsync(function(){
   $scope.schedule.scheduleType = 'One time';
})`
© www.soinside.com 2019 - 2024. All rights reserved.