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
/* 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}}