$ watchGroup VS $ watchCollection? [英] $watchGroup vs $watchCollection?

查看:95
本文介绍了$ watchGroup VS $ watchCollection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2种方式观看一组变量的在角。但是,什么是它们之间的区别?

There're 2 ways to watch a group of variables in Angular. But what's the difference between them?

他们似乎都做浅手表。有没有的情况下,其中一个显然是preferable比其他?

They both seem to do shallow watches. Are there situations where one is obviously preferable over the other?

推荐答案

$ watchCollection 将浅手表的属性的单个对象的并通知你如果其中的一个改变。

$watchCollection will shallow watch the properties on a single object and notify you if one of them changes.

$ watchGroup 手表一组独立的手表前pressions的。

$watchGroup however watches a group of individual watch expressions.

它们不是功能等效。 $ watchGroup 可以用来当你想要观看的前pressions应该都到同一个回调回应众多 - 这可能是个别前pressions了针对不同的对象。这可以被用于看'富''foo.bar' baz.qux。这是3个不同的目标 - 财产和的巴兹 qux 属性,但它们将全部委托给相同的处理。

They are not functionally equivalent. $watchGroup could be used when you want to watch a multitude of expressions that should all respond to the same callback - these could be individual expressions that target different objects. This could be used for watching 'foo', 'foo.bar', and 'baz.qux'. These are 3 different targets - foo, foo's bar property and baz's qux property, but they will all delegate to the same handler.

相反, $ watchCollection 只会浅浅的手表一个对象。这使得它更好地为一个对象,可能会改变它的属性很多(例如 - 我们自己的 $范围)。在与我们的垃圾实例名称保持一致,以达到与上面相同,你只会想观看巴兹,但你会在通知的任何变化和巴兹(而不是仅仅被通知有关<变更code>富本身, foo.bar baz.qux

By contrast, $watchCollection will only shallow watch a single object. This makes it better for an object that might change it's properties a lot (for example - our very own $scope). In keeping with our rubbish name examples, to achieve the same as above you would only want to watch foo and baz, but you would be notified of any changes in foo and baz (as opposed to just being notified for the changes on foo itself, foo.bar and baz.qux.

这篇关于$ watchGroup VS $ watchCollection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆