Running $apply on $rootScope vs any other scope

前端 未结 4 2115
故里飘歌
故里飘歌 2021-01-07 16:11

The $apply function can run on any scope, including $rootScope.

Are there cases when it makes a difference if I run it on my local scope or

4条回答
  •  被撕碎了的回忆
    2021-01-07 16:48

    /* What happens with $apply */ 
    angular.module('myApp',[]).controller('MessageController', function($scope) {
        
          $scope.getMessage = function() {
            setTimeout(function() {
              $scope.$apply(function() {
                //wrapped this within $apply
                $scope.message = 'Fetched after 3 seconds'; 
                console.log('message:' + $scope.message);
              });
            }, 2000);
          }
          
          $scope.getMessage();
        
        });
    
    
          
    Delayed Message: {{message}}

提交回复
热议问题