I tried using async/await to avoid *ngIf
in the component template.
I'm getting an error in the Chrome console when I remove *ngIf
in component template.
Can anyone help to get a solution. I do not want to use *ngIf
in the component template as a requirement
I'm calling the GET REST API and used subscribe. Also, I'm getting results without the error if I'm using *ngIf
, but I want the same results without using *ngIf
CODE: component.ts
export class DisplayComponent implements OnInit {
users : user[];
user: user;
userResult: any;
constructor(private http: HttpClient) { }
ngOnInit() {
}
async readUser(id){
this.userResult = await this.http.get<user>("http://localhost:8090/user/" +id).subscribe(
data => {
this.user = data;
},
error =>{
return console.error();
;
}
)
console.log("Fuuhhh! Client will wait till promise is resolved.");
}
}
CODE: DisplayComponent.html
<div style="text-align: center;">
<button (click)="loadUsers()">load Users</button>
<table style="margin: 0 auto;" border="1">
<tr>
<th>Identity</th>
</tr>
<tr *ngFor="let user of users">
<td>
<button style="background: transparent;border: transparent;cursor: pointer;" (click)="readUser(user.id)">{{user?.id}}</button>
</td>
</tr>
</table>
</div>
<div style="text-align: center;">
<span style="color: darkgray;">Identity:</span> <span style="color: gray;">{{user.id}}</span> <br>
<span>Name:</span> {{user.name}} <br>
<span>School:</span> {{user.school}} <br>
</div>
Thanks, Folks for trying to help me out in different ways. But yes I didn't get a resolution from the answers.
Finally, I got the solution myself doing some R&D on "avoiding *ngIf", and following is the in-short solution:
Using Angular "Resolver" service I got the data before rendering the template/View.