ngModel中的异步数据 [英] Async data in ngModel

查看:67
本文介绍了ngModel中的异步数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务器绑定了带有异步数据的表单:

I have form with async data binded from server:

<form>
 <input [(ngModel)]="currentService.user.username">
 <input [(ngModel)]="currentService.user.email">
</form>

仅当我在表单中添加*ngIf="currentService.user"时它才有效,但是在我从服务器获取数据之前,我想渲染无值的输入.但是我得到了错误:

It's work only if i add *ngIf="currentService.user" to form, but i want render inputs without value, before i get data from server. But i get errors:

无法读取未定义的属性"用户名"

'Cannot read property 'username' of undefined'

我该如何解决?也许,我需要类似ngModel的异步管道,但这显然行不通.

How can i solve this? Probably, i need something like async pipe to ngModel, but this is does not work, obviously.

推荐答案

您可以尝试一下

 <input [(ngModel)]="currentService.user && currentService.user.username">
 <input [(ngModel)]="currentService.user && currentService.user.email">

这篇关于ngModel中的异步数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆