如何在角度 4 中使用 NgIf 设置输入字段的值



我对 Angular 4 很陌生,对于比我更有经验的人,我可能有基本问题。

我有输入字段,当对象用户存在时需要填充该值。所以基本上我需要 php 函数 isset() 在角度 4 中。

我试过这样,但它不起作用。

<input class="form-control" type="text"  value="{{ !!user.firstname ? user.firstname : null }}" id="firstname">

任何帮助将不胜感激。

您应该将 [(ngModel)] 与输入一起使用,并使用 elvis 运算符来检查用户是否存在或使用*ngIf

<input class="form-control" type="text"  [(ngModel)]=user?.firstName id="firstname">

试试这个:

<input class="form-control" type="text" [value]="user.firstname ? user.firstname : null"  name="firstname" id="firstname">

最新更新