$watch, $watchCollection, $watchGroup的使用
官方文档
$watch简单使用
$watch是一个scope函数,用于监听模型变化,当你的模型部分发生变化时它会通知你。
$watch(watchExpression, listener, objectEquality);
每个参数的说明如下:
watchExpression:监听的对象,它可以是一个angular表达式如'name',或函数如function(){return $scope.name}。
listener: 当watchExpression变化时会被调用的函数或者表达式,它接收3个参数:newValue(新值), oldValue(旧值), scope(作用域的引用)
objectEquality:是否深度监听,如果设置为true,它告诉Angular检查所监控的对象中每一个属性的变化. 如果你希望监控数组的个别元素或者对象的属性而不是一个普通的值, 那么你应该使用它
举个例子:
$scope.name = 'hello';
var watch = $scope.$watch('name',function(newValue,oldValue, scope){
console.log(newValue + ' - ' + oldValue);
});
//1秒后改变name变量的值
$timeout(function(){
$scope.name = "world";
},1000);
如何注销$watch
太多的$watch将会导致性能问题,$watch如果不再使用,我们最好将其释放掉。
$watch函数返回一个注销监听的函数,如果我们想监控一个属性,然后在稍后注销它,可以使用下面的方式:
var counter = 0;
var watch = $scope.$watch('name',function(newValue,oldValue, scope){
console.log('new:' + newValue + ' old:' + oldValue);
counter ++;
//超出停止注销watch
if (counter > 5 ) {
watch();
}
});
$watchGroup(watchExpressions, listener);
watchExpressions是数组类型
如果要监听多个变量就要写很多个watch,这显然不是很好的作用。
使用$watchGroup可同时监听多个变量,如果任一一个变量发生变化就会触发listener。
$scope.teamScore = 0;
$scope.time = 0;
$scope.$watchGroup(['teamScore', 'time'], function(newVal, oldVal) {
if(newVal[0] > 20){
$scope.matchStatus = 'win';
}
else if (newVal[1] > 60){
$scope.matchStatus = 'times up';
});
$watchCollection(obj, listener);
Shallow watches the properties of an object and fires whenever any of the properties change (for arrays, this implies watching the array items; for object maps, this implies watching the properties). If a change is detected, the listener callback is fired.
针对对象属性的浅层监视(Shallow Watch),当属性发生变化时触发(对于数组,指的是监视数组的元素;对于字典对象,指的是监视其属性) 触发listener的回调操作。
其实完全可以使用$watch,这里是对性能的考虑。
关于这三个方法的 在线例子
另外注意的是这三个方法貌似是1.3版本之后才加入的。
stackoverflow上有人做了对比,我测试了几个是正确的。
$watch() will be triggered by:
$scope.myArray = [];
$scope.myArray = null;
$scope.myArray = someOtherArray;
$watchCollection() will be triggered by everything above AND:
$scope.myArray.push({}); // add element
$scope.myArray.splice(0, 1); // remove element
$scope.myArray[0] = {}; // assign index to different value
$watch(..., true) will be triggered by EVERYTHING above AND:
$scope.myArray[0].someProperty = "someValue";
JUST ONE MORE THING...
$watch() is the only one that fires when an array is replaced with another with the same exact content. For example:
$scope.myArray = ["Apples", "Bananas", "Orange" ];
var newArray = [];
newArray.push("Apples");
newArray.push("Bananas");
newArray.push("Orange");
$scope.myArray = newArray;
Below is a link to an example JSFiddle that uses all the different watch combinations and outputs log messages to indicate which "watches" were triggered:
http://jsfiddle.net/luisperezphd/2zj9k872/

个人总结:
1. $watchGroup 的第一个参数要传数组,$watchCollection的第一个参数要传对象
2. 监视对象和数组而且监视层次不是特别深,优先使用$watchCollection, $watchCollection可以方便的监视数组的插入,移除。
3. 要同时监视多个变量并执行同一逻辑使用$watchGroup
4. 除此一般情况下使用$watch,如果你知道数据结构的深度,可以直接这样监视。 当第三个参数true,表示深度监测,如果watch的变量比较复杂,效率会变低。
$watch('user.data', (newVal, oldVal) => {
if(newVal !== oldVal) {
}
})
参考:
http://stackoverflow.com/questions/26535415/angularjs-watch-vs-watchcollection-which-is-better-for-performance
http://blog.csdn.net/dm_vincent/article/details/51620193
http://blog.csdn.net/u010451286/article/details/50635839
随机推荐
- 转:Eclipse+webservice开发实例
原文地址:http://blog.csdn.net/xw13106209/article/details/7049614 1.参考文献: 1.利用Java编写简单的WebService实例 http ...
- [翻译] iOSSharedViewTransition
iOSSharedViewTransition iOS 7 based transition library for View Controllers having a Common View 基于i ...
- 电脑技巧——DOS和windows的区别?
本质:都是微软公司的操作系统,某种从程度上说windows是dos的后续操作系统版本.只是windows相比dos有质的飞跃.dos只支持命令操作,windows则有了良好的图形操作界面,window ...
- BlockingQueue笔记
Concurrent包中的BlockingQueue很好的解决了多线程中如何高效安全“传输”数据的问题.通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的便利. 通过一个共享 ...
- C++ 课堂作业1.0
c++第一次课堂作业点这里 题目要求:输入半径,计算圆的面积,在调用外部函数,无需使用类.
- Connection:Keep-alive
名词解释: HTTP无状态:无状态是指协议对于事务处理没有记忆能力,服务器不知道客户端是什么状态.从另一方面讲,打开一个服务器上的网页和你之前打开这个服务器上的网页之间没有任何联系 如果你要实现一个购 ...
- Rabbitmq.md
RabbitMQ介绍 什么是RabbitMQ RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性.扩展性.高可用性等方面 ...
- 对于高并发短连接造成Cannot assign requested address解决方法
https://www.cnblogs.com/dadonggg/p/8778318.html 感谢这篇文章给予的启发 在tcp四次挥手断开连接时,主动释放连接的一方最后会进入TIME_WAIT状态, ...
- HBase学习之路 (十)HBase表的设计原则
建表高级属性 下面几个 shell 命令在 hbase 操作中可以起到很大的作用,且主要体现在建表的过程中,看 下面几个 create 属性 1. BLOOMFILTER 默认是 NONE 是否使用布 ...
- (十)T检验-第一部分
介绍T分布.T检验.Z检验与T检验.P值.相依样本以及配对样本的非独立T检验. T分布 在到目前为止举的所有例子中,我们都假设我们知道总体参数 μ 和 σ,但很多时候,我们并不知道,我们通常只有样本, ...