$digest 的次序 evalAsync, timeout, location

 

$digest 的顺序 : 

1. evalAsync(fn) 的fn 会延长到下一次digest才触发, before dirty check 所有的fn会运行,运行期间产生的evalAsync也会同时被执行,它是一个loop

The $evalAsync makes no guarantees as to when the expression will be executed, only that:

  • it will execute after the function that scheduled the evaluation (preferably before DOM rendering).
  • at least one $digest cycle will be performed after expression execution.

 

while (asyncQueue.length) {              
try {
    asyncTask = asyncQueue.shift();
    asyncTask.scope.$eval(asyncTask.expression, asyncTask.locals);
} catch (e) {
    $exceptionHandler(e);
}
lastDirtyWatch = null;
}

//dirty check...

 

2. location change 会先于 evalAsync 

   $digest: function () {
          log("digest");
        var watch, value, last,
            watchers,
            length,
            dirty, ttl = TTL,
            next, current, target = this,
            watchLog = [],
            logIdx, logMsg, asyncTask;

        beginPhase('$digest');
        // Check for changes to browser url that happened in sync before the call to $digest
        $browser.$$checkUrlChange();

        if (this === $rootScope && applyAsyncId !== null) {
          // If this is the root scope, and $applyAsync has scheduled a deferred $apply(), then
          // cancel the scheduled $apply and flush the queue of expressions to be evaluated.
          $browser.defer.cancel(applyAsyncId);
          flushApplyAsync();
        }

 

3. $timeout 会等到dirty check 之后才运行

//dirty check finish ..

while
(postDigestQueue.length) { try { postDigestQueue.shift()(); } catch (e) { $exceptionHandler(e); } }

 

 

 

posted @ 2015-10-18 11:08  兴杰  阅读(248)  评论(0)    收藏  举报