'angular ng if with async data?
In my angular navbar I have a *ngIf for rather or not login/logout button should be showing
*ngIf="!authService.loggedIn()
it uses a service with this loggedIn function that returns true or false
loggedIn() {
return this.http.get('http://localhost:3000/users/validateToken')
.map(res => res.json()).map(data => data.success);
}
bu this is async, how I make my ngIf await it ? or how can I differently get the same result ?
Solution 1:[1]
You can use the async pipe since if request returns a observable.
*ngIf="!authService.loggedIn() | async"
Solution 2:[2]
Seems like
*ngIf="asyncfn() | async"
Only leads to an infinit loop, however:
constructor() {
this.result = this.asyncFn();
}
public asyncFn() { // ... }
template:
*ngIf="result | async"
works fine... lovely Angular... how the heck would you defer it then to only done when needed? Does Angular team hate promises?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sachila Ranawaka |
| Solution 2 | OZZIE |
