AngularJS 视图不会在模型更改时更新 [英] AngularJS view not updating on model change
本文介绍了AngularJS 视图不会在模型更改时更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试弄清楚 Angular 的工作原理,但在模型更改时无法更新我的视图..
HTML
<p ng-controller="TestCtrl">{{测试值}}</p>
JS
var app = angular.module('test', []);app.controller('TestCtrl', function ($scope) {$scope.testValue = 0;设置间隔(函数(){console.log($scope.testValue++);}, 500);});
有什么想法吗?
解决方案
正如 Ajay beniwal 上面提到的,你需要使用 Apply 来开始消化.
var app = angular.module('test', []);app.controller('TestCtrl', function ($scope) {$scope.testValue = 0;设置间隔(函数(){console.log($scope.testValue++);$scope.$apply()}, 500);});
i'm trying to figure out how Angular works and am having trouble getting my view to update when the model changes..
HTML
<div ng-app="test">
<p ng-controller="TestCtrl">
{{testValue}}
</p>
</div>
JS
var app = angular.module('test', []);
app.controller('TestCtrl', function ($scope) {
$scope.testValue = 0;
setInterval(function() {
console.log($scope.testValue++);
}, 500);
});
any ideas?
解决方案
As Ajay beniwal mentioned above you need to use Apply to start digestion.
var app = angular.module('test', []);
app.controller('TestCtrl', function ($scope) {
$scope.testValue = 0;
setInterval(function() {
console.log($scope.testValue++);
$scope.$apply()
}, 500);
});
这篇关于AngularJS 视图不会在模型更改时更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文