0
votes

The setup:

  • A parent component has a list of filter values (small, medium, big)
  • A child component has a list of items that should be filtered according to the value selected in the parent component

is it possible for the parent component to expose an Observable and the child component to subscribe to it?

It is not clear to me, using [filter]="Observable", at what point to subscribe.

parent component .ts file:

...
@Output filterObservable: Observable<string>;
...

in parent component HMTL template:

...
<child-component [filter]="filterObservable" />
...

child component ts file:

@Input filter: Observable<string>;

At some point, the child component needs to subscribe (make this call)

this.filter.subscribe(msg => console.log(msg));

Which life cycle event is the appropriate one to do the Observable subscription? Do I have to check in every ngOnChanged event whether the filter value is different and do unsubscribe/subscribe again (or call the switch opration)?

1
Could you please share a piece of code that explains what you want to achieve? - Ahmet Emrebas
Ahmet, I have updated the question. I hope it is clearer now. - Nafas

1 Answers

1
votes

Answer:

parent component .ts file:

...
@Output filterObservable: Observable<string>;
...

in parent component HMTL template:

...
<child-component [filter]="filterObservable | async" />
...

child component ts file:

@Input filter: string | null;