I am trying to show and hide HTML elements according to the service result. I am using *ngIf="messageService.getData() | async" but it isn't working to show or hide the element. I am using async, otherwise "Failure Message" is shown for a short time and then "Successful Message" is shown.
I have 2 tags like these:
<div *ngIf="messageService.getData() | async">Successful Message</div>
<div *ngIf="!messageService.getData() | async">Failure Message</div>
In the service I have a code like this:
export class MessageService {
constructor(private http: HttpClient) { }
public getData() {
return this.http.get("https://jsonplaceholder.typicode.com/todos/1")
.pipe(
map((response) => {
console.log("success");
}),
catchError(this.handleError)
)
}
private handleError(error: Response) {
console.log("handleError")
let errMsg: string;
errMsg = "error"
return Observable.throw(errMsg);
}
}
Here is the source code: https://stackblitz.com/edit/angular-iqr6az