Angular2未检测到Array中的更改 [英] Angular2 doesn't detect changes in Array
问题描述
我正在进行Ionic2 / Angular2项目。我有一个
I am on an Ionic2 / Angular2 project. There I have a
* ngFor =让项目项目| async | customPipe
在我的代码中。 异步是因为项目
是 Observable< Item []>
即可。我的 customPipe
在第一次运行时运行正常。但是,当我通过我的 customPipe
过滤掉它的项目时,我对其进行了任何更改。
in my code. The async is because items
is an Observable<Item[]>
. My customPipe
is working fine on the first run. But when I make any change to one if the items that would filter it out through my customPipe
it is still shown.
问题是什么? * ngFor
只运行一次吗?或者我是否必须强制进行DOM更新?感谢您的帮助。
What's the problem? Is the *ngFor
only run once? Or do I have to force a DOM-update? Thanks for any help.
推荐答案
根据Angular2管道文档:
According to Angular2 pipe docs:
只有在检测到输入值的纯变化时,Angular才会执行纯管道。纯变化是对原始输入值(String,Number,Boolean,Symbol)的更改或更改的对象引用(Date,Array,Function,Object)。
Angular executes a pure pipe only when it detects a pure change to the input value. A pure change is either a change to a primitive input value (String, Number, Boolean, Symbol) or a changed object reference (Date, Array, Function, Object).
Angular忽略(复合)对象内的更改。如果我们更改输入月份,添加到输入数组或更新输入对象属性,它将不会调用纯管道。
Angular ignores changes within (composite) objects. It won't call a pure pipe if we change an input month, add to an input array, or update an input object property.
原因:
这看起来似乎有限制,但速度也很快。对象引用检查很快 - 比深度检查差异要快得多 - 因此Angular可以快速确定它是否可以跳过管道执行和视图更新。
This may seem restrictive but is is also fast. An object reference check is fast — much faster than a deep check for differences — so Angular can quickly determine if it can skip both the pipe execution and a view update.
因此,当您更改数组或数组中的元素时,数组引用不会更改。
So while you change your array or the elements inside the array, the arrays reference doesn't change.
因此切换到 .onPush
或手动触发更改检测可能会解决您的问题。
So switching to .onPush
or triggering the change detection manually might solve your problem.
来源: https://angular.io/docs/ts/latest/guide/pipes.html #!#pure-and-impure-pipes
这篇关于Angular2未检测到Array中的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!