2017-08-03 37 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"