javascript - Why is the Angular digest loop different on UI events, $timeout and $http? -
i have been working on lib angular, using 1.2.27, , surprise looking @ result:
$scope.myclickmethod = function () { $scope.$$phase; // $apply $scope.$apply(); // gives digest error, of course }; $timeout(function () { $scope.$$phase; // null $scope.$apply(); // not give digest error }); $http.get('/').then(function () { $scope.$$phase; // $digest $scope.$apply(); // gives digest error });
the problem can not identify digest loop ready run when using $timeout, $http etc. ng-clicks etc. questions is... why different? thought angular did in both cases:
$scope.myclickmethod = function angularwrapper () { $rootscope.$apply(function myoriginalmethod () { // code }); }; $timeout(function angularwrapper () { $rootscope.$apply(function myoriginaltimoutfunction () { // code }); });
why $$phase = $apply on ui-events, $$phase = $digest on $http , $$phase = null on $timeout? async in nature, not make sense behave differently. sure there reason though. know?
i see 1.3 has changes $http new $applyasync method. not tested though.
the $timeout
callback called @ end of digest loop.
so, because outside digest loop, can call $apply
, recall entire digest loop.
for other methods (ng-click
, $http
), called during digest loop. calling $apply
give error because angularjs doesn't want digest loop in digest loop.
Comments
Post a Comment