Angular 5 ngOnChanges fires only once in child component

前端 未结 2 1299
不思量自难忘°
不思量自难忘° 2021-01-19 21:47

I have a child component that contains complex input (some chart data). I am sending this chart data from the parent component through the @Input() decorator in Angular 5 af

2条回答
  •  时光说笑
    2021-01-19 22:51

    Thanks to @HoangDucNguyen, here's the working code:

    this.chartService.getChartData(params).subscribe(res => {
      this.chartData = res;
      this.changeDetectorRef.detectChanges();
    });
    

    Of course you will need to import the ChangeDetectorRef class from @angular/core and inject it in your constructor.

    Explanation:
    When variables are changed outside of the angular context (in here: it's the rxJs context), Angular isn't able to detect changes on the variables. So, you need to manually fire the detect changes angular method.

    Official doc: https://angular.io/api/core/ChangeDetectorRef

提交回复
热议问题