How to detect when an @Input() value changes in Angular?

Discussão em 'Angular' iniciado por Jon Catmull, Outubro 30, 2024.

  1. Jon Catmull

    Jon Catmull Guest

    I have a parent component (CategoryComponent), a child component (videoListComponent) and an ApiService.

    I have most of this working fine i.e. each component can access the json api and get its relevant data via observables.

    Currently video list component just gets all videos, I would like to filter this to just videos in a particular category, I achieved this by passing the categoryId to the child via @Input().

    CategoryComponent.html

    <video-list *ngIf="category" [categoryId]="category.id"></video-list>


    This works and when the parent CategoryComponent category changes then the categoryId value gets passed through via @Input() but I then need to detect this in VideoListComponent and re-request the videos array via APIService (with the new categoryId).

    In AngularJS I would have done a $watch on the variable. What is the best way to handle this?

    Continue reading...

Compartilhe esta Página