且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Angular2 中动态更新 [已检查]

更新时间:2023-11-26 16:36:52

那是因为如果你使用 push 向数组添加新项,那么在执行纯管道时,数组引用不会改变仅当它检测到对输入值的纯粹更改(在您的情况下为 arrayvalue)

That's because if you use push to add new item to array then the array reference isn't changed while pure pipe will be executed only when it detects a pure change to the input value (array and value in your case)

有两种解决方案:

1) 返回新数组

this.array = this.array.concat(4)

this.array = [...this.array, 4];

Plunker

2) 使用不纯的管道

@Pipe({name: 'contains', pure: false})
export class ContainsPipe implements PipeTransform {

Plunker

更多详情请见