Understanding change detection in angular 2

前端 未结 2 419
一向
一向 2021-01-21 10:13

I have come across below example in Angular 2 documentation

@Component({
  selector: \'cmp\',
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `Num         


        
2条回答
  •  一生所求
    2021-01-21 10:31

    Using ChangeDetectionStrategy.OnPush tells Angular not to perform change detection on your component (i.e. updating its view) unless one or more of the component's inputs has changed (these inputs should be immutable objects).

    For any events that come from within the component itself and require the view to be updated, you have to explicitly tell the change detector to look for changes in that component on its next change detection run.

    In this snippet, ref is a reference to the change detector. Calling ref.markForCheck() tells the change detector that something has happened that will change the view (i.e. numberOfTicks has been incremented) and it needs to recompute it.

提交回复
热议问题