我想大部分的初學者都會經歷過一個狀況,也就是在一個 Parent 和 child scope 間突然失去了 binding
可以看看以下的範例
當第一次改變外部的 input,在 directive 中的 input 也會變動,看起來非常正常
但移到內部的 input 修改值後,再回去外部的 input 輸入值,發現 binding 失效
這原因是 ng-switch 會產生一個新的 scope 然後在 scope object 上面直接 bind thefiled
這會讓他 overwrite parent 的 thefiled,因此就失連囉
最佳的做法應該是要確保我們不可以直接 bind model to scope property
而是要讓 scope 去 reference 自己定義的 model object(plain old object)
http://jsfiddle.net/MrFusion/r4RKW/36/
結論就是:
在view上的ng-model 請盡量寫成有 dot 的寫法,另外 keep view scope read only
文章標籤
全站熱搜
