Reload child component when variables on parent component changes. Angular2

后端 未结 5 685
粉色の甜心
粉色の甜心 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 01:47

    update of @Vladimir Tolstikov's answer

    Create a Child Component that use ngOnChanges.

    ChildComponent.ts::

    import { Component, OnChanges, Input } from '@angular/core';
    import { ActivatedRoute } from '@angular/router';
    
    @Component({
      selector: 'child',
      templateUrl: 'child.component.html',
    })
    
    export class ChildComponent implements OnChanges {
      @Input() child_id;
    
      constructor(private route: ActivatedRoute) { }
    
      ngOnChanges() {
        // create header using child_id
        console.log(this.child_id);
      }
    }
    

    now use it in MasterComponent's template and pass data to ChildComponent like:

    
    

提交回复
热议问题