$ watchGroup VS $ watchCollection? [英] $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屋!