【发布时间】:2020-05-30 10:21:32
【问题描述】:
我尝试使用 async/await 来避免组件模板中出现*ngIf。
当我在组件模板中删除 *ngIf 时,Chrome 控制台出现错误。
任何人都可以帮助获得解决方案。我不想在组件模板中使用*ngIf 作为要求
我正在调用 GET REST API 并使用订阅。此外,如果我使用*ngIf,我会得到没有错误的结果,但我想要不使用*ngIf 的相同结果
代码: 组件.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.");
}
}
代码: 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>
【问题讨论】:
-
请发布您的代码和错误信息。
-
没问题。
-
我没有看到你在模板中使用 *ngIf
-
@DanielF 请检查问题中的代码。
-
ngIf 在哪里??
标签: angular async-await angular-ng-if