【问题标题】:Update Angular2 component only once after multiple onNgChange多次 onNgChange 后仅更新一次 Angular2 组件
【发布时间】:2017-06-06 09:17:20
【问题描述】:

我有一个带有ChangeDetectionStrategy.OnPush 和多个@Input 属性的组件。

当多个属性同时更改时,我会多次调用ngOnChanges

我需要在所有ngOnChanges 完成后重新计算大小(对于那个滴答/摘要/更改检测周期)。

所以不是有

ngOnChanges(a) -> recalculate()
ngOnChanges(b) -> recalculate()
ngOnChanges(c) -> recalculate()

我想要

ngOnChanges(a)
ngOnChanges(b)
ngOnChanges(c)
recalculate()

setTimeout 可能会起作用,但我想知道如何使用 ng2 工具正确地做到这一点。

【问题讨论】:

    标签: typescript angular2-components angular2-changedetection


    【解决方案1】:

    试试这个:

    ngOnChanges(changes: SimpleChange}) {
       if(changes['a'] && changes['b'] && changes['c']){
         recalculate();
       }
      }
    

    【讨论】:

    • 当然ngOnChanges 不会同时包含所有更改
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-10
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多