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 }
问题解决~
浙公网安备 33010602011771号