angular attrs.$observe和$scope.$watch的區別


  http://stackoverflow.com/questions/14876112/difference-between-the-observe-and-watch-methods

https://docs.angularjs.org/api/ng/type/$compile.directive.Attributes

$observe()是Attributes 對象的一個方法,它只能用於observe/watch一個DOM attribute的value change.它只在directive內部被調用。當你需要監察、偵聽一個包含interpolation的attribute的DOM時,你需要使用$observe()函數(比如:{{attrValue}},再比如select控件,當你選擇一個option時,如果對應的屬性作為觸發過濾結果集這種場景時,可以$observe這個選擇動作的結果(作為attr)

<div d1 attr1="{{prop1}}-test" attr2="prop2" attr3="33" attr4="'a_string'"
        attr5="a_string" attr6="{{1+aNumber}}"></div>

 

$watch被用於監察、偵聽一個model/scope屬性,比如:scope.$watch(attrs.attr1)...

注意:angular normalize string


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM