码迷,mamicode.com
首页 > 移动开发 > 详细

$apply() $digest()

时间:2015-10-26 18:39:45      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:

理解Angular中的$apply()以及$digest()

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body ng-app="myApp">
    <div ng-controller="MessageController">
    Delayed Message: {{message}}
    </div>
    你应该使用$timeout service来代替setTimeout(),因为前者会帮你调用$apply(),让你不需要手动地调用它
    <script src="angular.min.js"></script>
    <script>   
    angular.module(myApp,[]).controller(MessageController, function($scope) {
    
      $scope.getMessage = function() {
           /*
        setTimeout(function() {
          $scope.message = ‘Fetched after 3 seconds‘;
          console.log(‘message:‘+$scope.message);
        }, 2000);
        */

        setTimeout(function() {  
          $scope.$apply(function() {  
            //wrapped this within $apply  
            $scope.message = Fetched after 3 seconds;   
            console.log(message: + $scope.message);  
          });  
        }, 2000);  
      }
      
      $scope.getMessage();
    
    });
    </script>
</body>
</html>

 

$apply() $digest()

标签:

原文地址:http://www.cnblogs.com/jzm17173/p/4911730.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!