我有带有异步数据的表单:
<form>
<input [(ngModel)]="currentService.user.username">
<input [(ngModel)]="currentService.user.email">
</form>
它只有在我将 *ngIf="currentService.user"
添加到表单时才有效,但我想在从服务器获取数据之前呈现没有值的输入。但是我收到以下错误:
'Cannot read property 'username' of undefined'
我该如何解决这个问题?可能我需要类似 async
的管道到 ngModel
,但这显然行不通。
最佳答案
你可以试试这个
<input [(ngModel)]="currentService.user && currentService.user.username">
<input [(ngModel)]="currentService.user && currentService.user.email">
Working Example
关于angular - ngModel 中的异步数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50389639/