Reload child component when variables on parent component changes. Angular2

后端 未结 5 694
粉色の甜心
粉色の甜心 2020-12-24 01:30

I have a MasterComponent which loads header, footer, sidebar etc. On the header there is a dropdown whose options are set once the user is logged in. I want the header to be

5条回答
  •  被撕碎了的回忆
    2020-12-24 02:11

    On Angular to update a component including its template, there is a straight forward solution to this, having an @Input property on your ChildComponent and add to your @Component decorator changeDetection: ChangeDetectionStrategy.OnPush as follows:

    import { ChangeDetectionStrategy } from '@angular/core';
    
    @Component({
        selector: 'master',
        templateUrl: templateUrl,
        styleUrls:[styleUrl1],
        changeDetection: ChangeDetectionStrategy.OnPush    
    })
    
    export class ChildComponent{
      @Input() data: MyData;
    }
    

    This will do all the work of check if Input data have changed and re-render the component

提交回复
热议问题