[Fixed] Async data in ngModel


I have the form with async data:

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

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’

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


You could Try this out

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

Working Example

Leave a Reply

(*) Required, Your email will not be published