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

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

问题描述

我有包含异步数据的表单:

I have the form with async data:

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

仅当我将 *ngIf=currentService.user" 添加到表单时才有效,但我想在从服务器获取数据之前呈现没有值的输入.但我收到以下错误:

It works only if I add *ngIf="currentService.user" to the form, but I want to render inputs without value before I get data from the server. But I get the following error:

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

'Cannot read property 'username' of undefined'

我该如何解决这个问题?可能,我需要像 async 管道到 ngModel 之类的东西,但这显然行不通.

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

推荐答案

你可以试试这个

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

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

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