ngReact:解释了watch-depth属性类型

Cum*_*bus 7 angularjs reactjs ngreact

最近我发现了一个很棒的库,允许在Angular应用程序中使用React组件,称为ngReact

我的问题是关于可以在reactDirective组件上声明的watch-depth属性:

<body ng-app="app">
  <div ng-controller="helloController">
    <hello-component watch-depth="reference" fname="person.fname" lname="person.lname"></hello-component>
  </div>
</body>
Run Code Online (Sandbox Code Playgroud)

查看reactDirective服务的 ngReact文档,我看到watch-depth有3个可能的值:

  • 参考
  • 采集

在我使用ngReact的初步探索中,我一直坚持使用默认选项.

我的问题是,这些类型之间有什么区别?

当每个手表深度类型理想使用时的简单示例将是伟大的!

Ten*_*ink 5

答案与angular的$ watch如何工作有关。角度应用$ watch的方式有3种:引用,集合,值(如您已经提到的)。

参考:

引用查看值的引用,并且仅在该引用发生更改时才会注册更改(并导致重新呈现)。这是最便宜的手表类型。参考更改示例:

$scope.userArray = newUserArray
Run Code Online (Sandbox Code Playgroud)

采集:

深入了解Collection。它将在集合内部查看。如果将触发“按引用监视”,或者在集合中添加,删除或重新排序新项目,它将注册更改。

$scope.userArray.push(newUser);
Run Code Online (Sandbox Code Playgroud)

值:

深入了解价值是最昂贵的。它将监视集合中的值。如果将触发“按引用监视”,如果已触发“按集合监视”或集合中的值发生更改,它将注册更改。

$scope.userArray[0].age = 32;
Run Code Online (Sandbox Code Playgroud)

该答案的灵感来自Tero Parviainen撰写的出色文章, 网址为https://teropa.info/blog/2014/01/26/the-three-watch-depths-of-angularjs.html