$watch $apply and $evalAsync vs $timeout

$watch

$scope對象上的$watch方法會給Angular事件循環內的每一個$digest調用裝配一個髒值檢查。若是在表達式上檢測到變化, Angular老是會返回$digest循環。angularjs

$watch函數自己接受兩個必要參數和一個可選的參數:

  1.  watchExpression
    watchExpression能夠是一個做用域對象的屬性,或者是一個函數。在$digest循環中的每一個$digest調用都會涉及它。若是watchExpression是一個字符串, Angular會在$scope上下文中對它求值。若是它是一個函數,那麼Angular會認爲它會返回應該被監控的值。
  2.  listener/callback
    做爲回調的監聽器函數,它只會在watchExpression的當前值與先前值不相等(除了首次運行初始化期間)時調用。
  3.  objectEquality(可選)
    objectEquality是一個進行比較的布爾值,用來告訴Angular是否檢查嚴格相等。$watch函數會給監聽器返回一個註銷函數,咱們能夠調用這個註銷函數來取消Angular對當前值的監控。

$evalAsync vs $timeout

http://stackoverflow.com/questions/17301572/angularjs-evalasync-vs-timeout
To summarize:
if code is queued using $evalAsync from a directive, it should run after the DOM has been manipulated by Angular, but before the browser renders
if code is queued using $evalAsync from a controller, it should run before the DOM has been manipulated by Angular (and before the browser renders) -- rarely do you want this
if code is queued using $timeout, it should run after the DOM has been manipulated by Angular, and after the browser renders (which may cause flicker in some cases)app

$apply

http://blog.csdn.net/dm_vincent/article/details/38705099
若是你在AngularJS上下文以外的任何地方修改了model,那麼你就須要經過手動調用$apply()來通知AngularJS。這就像告訴AngularJS,你修改了一些models,但願AngularJS幫你觸發watchers來作出正確的響應。async

相關文章
相關標籤/搜索