Angular中的$watch、$watchGroup、$watchCollection
• 1,原型:$watch:function(watchExp,listener,objectEquality,prettyPrintExpression){};
• 2,参数:watchExp(必须):{(function()|string)},可以字符串表达式,也可以带当前scope为参数的函数
•-`string`:Evaluatedas{@linkguide/expressionexpression}
•-`function(scope)`:calledwithcurrent`scope`asaparameter.
•3,参数:listener(必须):function(newVal,oldVal,scope),观察的表达式变化的时候调用的函数。
•4,参数:objectEquality(非必须):是否监视个对象,默认为false
•5,$scope.$digest().会执行所有的同$scope下的$watch。
•但会出错$applyalreadyinprogress,换了$rootScope也一样。
•原因-参考大牛博客:http://blog.csdn.net/aitangyong/article/details/48972643
•$digest、$apply、$$phase这些属性或者方法其实都是$scope中的私有的,最好不要使用。
•6,$watch一个对象。
•如果要监视对象的变化(地址改变),$watch对象名,第三个参数默认;
•如果监测对象中某一属性,可以写user.name的形式,第三个参数默认;
•如果监测对象中全部属性,$watch对象名,第三个参数true;
•7,$watchGroup,第一个参数是一个表达式的数组或者返回表达式的数组的函数。
•8,$watchCollection;
•js中数组也是对象,但按照$watch一个对象的方式,只有数组引用变了才能监听变化,增加删除$watch监听不到,所以就有了$watchCollection。
•function(obj,listener):第一个参数必须对象或者返回对象的函数。
•9,注销$watch
• $watch函数返回一个注销监听的函数,太多的$watch将会导致性能问题,$watch如果不再使用,我们最好将其释放掉。
一、使用方法
html
$watch