I'm using a few REST services to gather data and then change the value of an Observable in my component code. I'm trying to use an *ngIf to show/hide div tags based on the result. However, it isn't working, the *ngIf is not updating based on the change to the Observable.
Do I have to trigger change detection? What's the best way to achieve this in Angular 7? Here's one example I tried:
HTML:
<div *ngIf="isAnimal$">
<p>animal</p>
</div>
Component:
public isAnimal$: Observable<boolean> = of(false);
...
ngOnInit() {
checkAnimal();
}
private checkAnimal() {
this._dataService.getData()
.subscribe((result) => {
if (result === 'animal') {
this.isAnimal$ = of(true);
}
});
}
I also tried creating a simple service and that didn't work either. Like this: Angular *ngIf binding to function