Can somebody explain me, why in the following example the @Input
is null if I access it in ngOnInit
of OtherComponent (the http call was successful)?
SomeComponent:
@Component({
selector: 'some-component',
template: `<other-component [data]="observable$ | async"></other-component>`,
styles: [``] })
export class SomeComponent {
observable$: Observable<any>;
ngOnInit(): void {
observable$ = this.http.get(...); // service call via angular httpClient
}
OtherComponent:
@Component({
selector: 'other-component',
template: `<div>...</div>`,
styles: [``] })
export class OtherComponent {
@Input()
data: any;
ngOnInit(): void {
// if I access this.data here, it is null
}
According to the angular documentation the async pipe subscribes to an observable and returns the latest value it has emitted. So I thought, the async pipe will pass down the result of the http call and not null.
If I use *ngIf, the result of the http call is passed down and not null.
<other-component *ngIf="observable$ | async as observable" [data]='observable'>
Stackblitz example: