2017-08-03 81 views
0

當null值到達JSON時,它將出現在輸入字段中。我如何隱藏它並不顯示它?並只顯示名稱中的含義?如何在輸入字段Angular 2中隱藏「null」?

Screenshot of fields showing null

<div> 
    <input type="hidden" 
     name="roleUserHidden-{{roleIndex}}" 
     #roleUser="ngModel" 
     [ngModel]="role.UserId === 'null' ? '' : role.UserId " /> 
    <input [disabled]="!permissions.canEdit" 
     class="form-control" 
     auto-complete 
     [ngModel]="role === 'null' ? '' : role" [source]="usersForAllocationSource.bind(this)" 
     list-formatter="Name" 
     name="roleUserAuto-{{roleIndex}}" 
     #roleUserVisible="ngModel" 
     (valueChanged)="usersForAllocationSelected(role, $event, roleUser)" 
     display-property-name="UserName" 
     [accept-user-input]="false" 
     (ngModelChange)="onRoleUserChange($event, role)" 
     [min-chars]="2"> 
</div> 
+0

https://stackoverflow.com/a/43094103/495157 - 使用指令 – JGFMK

+0

除非是字符串,否則不應該顯示'null'。 –

+0

在ts中檢查空值並分配給輸入ngModel – Sreemat

回答

2

簡單的解決方案:

<input name='name' [ngModel]="obj?.val"> 

另一種方式:

<input name='name' [ngModel]="obj.val ? obj.val : '' "> 

這裏是鏈接到plunker

+0

不工作(我添加代碼有問題請觀看( –

+0

嘗試用null替換'null'(不含引號) –

+0

是的,這是工作!謝謝! –

1

如果喲ü要隱藏整個輸入元素,你可以做*ngIf="value"值爲無論是空...

如果您希望值是一個空字符串,你可以做[value]="value === 'null' ? '' : value"

相關問題