Reload child component when variables on parent component changes. Angular2

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

    <child [child_id]="child_id"></child>
    
    0 讨论(0)
  • 2020-12-24 01:56

    In case, when we have no control over child component, like a 3rd party library component.

    We can use *ngIf and setTimeout to reset the child component from parent without making any change in child component.

    .template:

    .ts:

    show:boolean = true
    
    resetChildForm(){
       this.show = false;
    
       setTimeout(() => {
          this.show = true
        }, 100);
    }
    
    0 讨论(0)
  • 2020-12-24 01:58

    Use @Input to pass your data to child components and then use ngOnChanges (https://angular.io/api/core/OnChanges) to see if that @Input changed on the fly.

    0 讨论(0)
  • 2020-12-24 02:02

    You can use @input with ngOnChanges, to see the changes when it happened.

    reference: https://angular.io/api/core/OnChanges

    (or)

    If you want to pass data between multiple component or routes then go with Rxjs way.

    Service.ts

    import { Injectable } from '@angular/core';
    import { Observable, Subject } from 'rxjs';
    
    @Injectable({ providedIn: 'root' })
    export class MessageService {
      private subject = new Subject<any>();
    
      sendMessage(message: string) {
        this.subject.next({ text: message });
      }
    
      clearMessages() {
        this.subject.next();
      }
    
      getMessage(): Observable<any> {
        return this.subject.asObservable();
      }
    }
    

    Component.ts

    import { Component, OnDestroy } from '@angular/core';
    import { Subscription } from 'rxjs';
    
    import { MessageService } from './_services/index';
    
    @Component({
      selector: 'app',
      templateUrl: 'app.component.html'
    })
    
    export class AppComponent implements OnDestroy {
      messages: any[] = [];
      subscription: Subscription;
    
      constructor(private messageService: MessageService) {
        // subscribe to home component messages
        this.subscription = this.messageService.getMessage().subscribe(message => {
          if (message) {
            this.messages.push(message);
          } else {
            // clear messages when empty message received
            this.messages = [];
          }
        });
      }
    
      ngOnDestroy() {
        // unsubscribe to ensure no memory leaks
        this.subscription.unsubscribe();
      }
    }
    

    Reference: http://jasonwatmore.com/post/2019/02/07/angular-7-communicating-between-components-with-observable-subject

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题