我希望有个如你一般的人,如山间清爽的风,如古城温暖的光。从清晨到夜晚,由山野到书房。只要最后是你,就好。 ————张嘉佳

JS代码包住的angularJS代码scope对象不更新的问题

感谢https://www.cnblogs.com/penghongwei/p/3398361.html,先上代码

1 <div ng:app ng-controller="Ctrl">{{message}}</div>
1 functionCtrl($scope) {
2   $scope.message ="Waiting 2000ms for update";    
3   setTimeout(function () {
4     $scope.message ="Timeout called!";
5      // AngularJS unaware of update to $scope
6   }, 2000); 
7 }

上面的代码执行后页面上会显示:Waiting 2000ms for update。显然数据的更新没有被angular JS觉察到!!!!

接下来,我们将Javascript的代码稍作修改,用scope.scope.apply()包起来。

1 functionCtrl($scope) {
2   $scope.message ="Waiting 2000ms for update"; 
3   setTimeout(function () {
4     $scope.$apply(function () {
5        $scope.message ="Timeout called!";
6       });
7   }, 2000); 
8 }

问题解决~

posted on 2018-07-24 20:15  不曾放弃  阅读(400)  评论(0)    收藏  举报

导航