角度2管道触发

Ill*_*ian 6 angular2-template angular-pipe angular

我有一系列模特.在模板中我使用管道

<div class="card-panel" *ngFor="let card of cards | sortByType">
    <card-view [card]="card" [autoupdate]="true"></card-view>
</div>
Run Code Online (Sandbox Code Playgroud)

在每个组件中,我都有一个更新数据的计时器.但是当模型更新时,管道不再运行.在这种情况下,有没有办法强制运行管道或做一些有角度的方式.

在卡片视图中,我有一个更新卡片变量的计时器.但Angular没有抓住这个变化来触发管道

Gün*_*uer 15

  • 您可以创建一个cards比Angular2更改的副本更改检测检测到更改并再次执行管道.

  • 你可以使管道不纯净

@Pipe({ name: 'sortByType', pure: false})
Run Code Online (Sandbox Code Playgroud)

这会导致每次运行更改检测时都会执行管道.

您可以使用IterableDiffer执行自定义更改检测,并在数组实际未更改时返回缓存结果.

使用此选项时,必须小心不要导致严重的性能下降.

  • 您可以将其他参数传递给更新的管道(例如,每次更改阵列时都会增加的数字.

这样管道就会被调用,因为每当值或参数发生变化时,角度变化检测就会调用管道.

缺点是使用起来有点麻烦.