ngOnChanges not firing when input property changed

前端 未结 4 524
陌清茗
陌清茗 2020-12-17 19:30

Can you programatically trigger angular\'s change detection when mutating a component property in angular2?

@Compon         


        
4条回答
  •  借酒劲吻你
    2020-12-17 20:22

    The reason it doesn’t work can be found in the source code.

    https://github.com/angular/angular/blob/885f1af509eb7d9ee049349a2fe5565282fbfefb/packages/core/src/view/provider.ts

    Where ngOnChanges is called from, and where the SimpleChanges structure is built are very much tied into the component / directive code.

    It’s not just a ‘change tracker’ running that looks over every property however it was set, so ngOnChanges only works for bindings set by parent components.

    This is where ngDoCheck comes in and possibly KeyValueDiffers.

    See also:

    https://netbasal.com/angular-the-ngstyle-directive-under-the-hood-2ed720fb9b61 https://juristr.com/blog/2016/04/angular2-change-detection/

提交回复
热议问题