AngularJS 视图在模型更新后不会更新
AngularJS View does not update after model update
我的页面上有一个警报消息<div>
:
<div ng-controller="PercentageOverrideController as ctrl">
<script type="text/ng-template" id="alert.html">
<div class="alert" style="background-color:#fa39c3;color:white" role="alert">
<div ng-transclude></div>
</div>
</script>
<uib-alert ng-repeat="alert in alerts" type="{{alert.type}}" close="closeAlert($index)">{{alert.msg}}</uib-alert>
<button type="button" class='btn btn-default' ng-click="addAlert()">Add Alert</button>
</div>
在我的 Angular 控制器中,我有一个变量$scope.alerts = [ ]
和函数,它将新的警报消息推送到数组:
$scope.showSuccess = function(){
$scope.alerts.push({type: 'success', msg: 'Threshold Overrided'});
};
$scope.showError = function(){
$scope.alerts.push({type: 'danger', msg: 'Error has been happened'});
};
发出请求并获得响应后,我在调试模式下检查该函数是否已被调用,并且是否已将新值添加到数组中。但是它没有出现在我的页面上。
我的div
里有一个测试按钮:
<button type="button" class='btn btn-default' ng-click="addAlert()">Add Alert</button>
如果我按下此按钮,警报会出现在页面上。如果我尝试调用相同的函数:
$scope.addAlert = function() {
$scope.alerts.push({msg: 'Another alert!'});
};
在代码中:
$scope.showSuccess = function(){
$scope.alerts.push({type: 'success', msg: 'Threshold Overrided'});
$scope.addAlert();
};
但警报不会显示在页面上。
我假设我应该以某种方式触发视图以在页面上显示该更新。你们怎么看伙计们?谢谢!
您可以使用$scope.apply()
$scope.showSuccess = function(){
$scope.alerts.push({type: 'success', msg: 'Threshold Overrided'});
$scope.addAlert();
$scope.apply();
};
而不是<div ng-controller="PercentageOverrideController as ctrl">
试试<div ng-controller="PercentageOverrideController">
您需要
运行摘要循环。
$scope.$apply()
或者
$scope.$digest()
应该这样做。
相关文章:
- 视图ngValue-Angular JS中未更新模型的更改
- 需要使用触摸屏从 AngularJS 指令中更新模型
- 角度指令没有更新模型视图
- 复选框仅在第二次单击后更新模型
- Angular.js-Socket.io事件更新模型,而不是视图
- UI Select在删除后不会更新模型
- 为什么ng提交没有提交和更新模型
- AngularJS-基于URL更新模型
- 视图更改时,Ember.js更新模型
- Knockoutjs函数更新模型时应出现异常
- 挖空绑定不会更新模型
- 范围不更新模型中的更改
- 导航时更新模型变量
- 角度.js更新模型
- Ember.js不更新模型
- 在使用挖空自定义绑定时更新模型时更新元素
- 在自我更新时以ng重复更新模型
- 如何在主干中对列表进行排序时更新模型的顺序属性
- AngularJS 复选框不更新模型
- 更新模型中的数据不会绑定到控制器