问题描述
我是angular 2的新手,并试图使用angular 2基于模型动态生成一堆输入字段.一些字段是密码字段.如果要的话,我想使输入字段键入密码.我写了这样的东西:
I am new to angular 2 and trying to dynamically generate a bunch of input fields based on models using angular 2. Some fields are password fields. I want to make the input fields type to password if so. I have written something like this :
<div *ngFor="let field of connector.type.configFields">
<label>{{field.name}}</label>
<input [(ngModel)]="field.value" />
</div>
现在的问题是,如果field.name是password,我想输入字段密码我正在考虑在输入中添加这样的内容
now problem is I want to make the field password if the field.name is passwordI am thinking adding something like this to the input
<div *ngFor="let field of connector.type.configFields">
<label>{{field.name}}</label>
<input [(ngModel)]="field.value" *ngIf="field.name =='Password'" type="password" />
</div>
现在,如果我执行此操作,那么我所有不是密码的其他字段都不会被渲染.仅密码字段被呈现.我在做什么错.(我是angular2的新手)
now if I do this, All my other fields which are not password simply does not get rendered. Only password field gets rendered. What am I doing wrong.(i am very new to angular2)
推荐答案
这可能有用,但据我所知,不支持动态设置 type
(但值得一试):
This might work but as far as I remember, setting type
dynamically is not supported (but might be worth a try):
<input [(ngModel)]="field.value" [type]="field.name === 'Password' ? 'password' : 'text'"/>
这是安全的选项:
<input [(ngModel)]="field.value" *ngIf="field.name === 'Password'" type="password" />
<input [(ngModel)]="field.value" *ngIf="field.name !== 'Password'" type="text" />
如果 field.name
是'Password'
,则添加第一个,否则将第二个输入添加到DOM.
If field.name
is 'Password'
, the first is added, otherwise the 2nd input is added to the DOM.
这篇关于动态生成角度为2的输入字段类型并设置字段的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!