我有带有异步数据的表单:
<form>
<input [(ngModel)]="currentService.user.username">
<input [(ngModel)]="currentService.user.email">
</form>
仅当我添加时才有效*ngIf="currentService.user"
到表单,但我想在从服务器获取数据之前渲染没有值的输入。但我收到以下错误:
'无法读取未定义的属性'用户名''
我该如何解决这个问题?也许,我需要类似的东西async
管道到ngModel
,但这显然是行不通的。
你可以试试这个
<input [(ngModel)]="currentService.user && currentService.user.username">
<input [(ngModel)]="currentService.user && currentService.user.email">
工作示例 https://stackblitz.com/edit/async-ngmodel-binding?file=src/app/app.component.ts
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)