2017-10-20 69 views
0

我對Angular 4非常新穎,對於比我更有經驗的人,我可能會遇到一些基本問題。如何使用Angular 4中的NgIf設置輸入字段的值

我有輸入字段,當對象用戶存在時需要填寫哪個值。 所以基本上我需要php函數isset()在角度4.

我試過這樣,但它不工作。

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

任何幫助,將不勝感激。

TNX

+1

使用[ (ngModel)]而不是值 –

回答

1

您應該使用[(ngModel)與輸入和使用Elvis操作符來檢查該用戶是否存在,或者使用*ngIf

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

這是正確的,但形式稍有不同:

0

試試這個:

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