admin管理员组文章数量:1122832
I am defining the type of my form's fields in the ts, like following:
myFormFields = [{ label: 'Active', formControlName: 'active', value: false, type: 'checkbox' }]
Then in the html I'm trying to show them, like following:
<div class="form-floating mb-3">
<input type="{{field.type}}" formControlName="{{field.formControlName}}" />
<label class="form-label">
{{field.label}}
</label>
</div>
The problem is that, if I define the type of the input by type="{{field.type}}"
its value is not considered, i.e., it is never checked! But if I use type="checkbox"
, then it is checked when the value is true and non-checked if it is false.
I checked the Inspect, the only difference I could see was that, when input type is defined by field.type
it has value
property, like :
//(checkbox is not checked.)
<input _ngcontent-ng-c3053247743="" type="checkbox" ng-reflect-name="active"
ng-reflect-ng-class="[object Object]" value="true" class="ng-untouched ng-pristine ng-valid">
but when it is set to type='checkbox'
it does not have any value property:
//(checkbox is checked.)
<input _ngcontent-ng-c2417600516="" type="checkbox" ng-reflect-name="active"
ng-reflect-ng-class="[object Object]" class="ng-untouched ng-pristine ng-valid">
Does anyone know what is the problem? Thank you in advance.
I am defining the type of my form's fields in the ts, like following:
myFormFields = [{ label: 'Active', formControlName: 'active', value: false, type: 'checkbox' }]
Then in the html I'm trying to show them, like following:
<div class="form-floating mb-3">
<input type="{{field.type}}" formControlName="{{field.formControlName}}" />
<label class="form-label">
{{field.label}}
</label>
</div>
The problem is that, if I define the type of the input by type="{{field.type}}"
its value is not considered, i.e., it is never checked! But if I use type="checkbox"
, then it is checked when the value is true and non-checked if it is false.
I checked the Inspect, the only difference I could see was that, when input type is defined by field.type
it has value
property, like :
//(checkbox is not checked.)
<input _ngcontent-ng-c3053247743="" type="checkbox" ng-reflect-name="active"
ng-reflect-ng-class="[object Object]" value="true" class="ng-untouched ng-pristine ng-valid">
but when it is set to type='checkbox'
it does not have any value property:
//(checkbox is checked.)
<input _ngcontent-ng-c2417600516="" type="checkbox" ng-reflect-name="active"
ng-reflect-ng-class="[object Object]" class="ng-untouched ng-pristine ng-valid">
Does anyone know what is the problem? Thank you in advance.
Share Improve this question edited Nov 21, 2024 at 23:58 Naren Murali 54.6k5 gold badges40 silver badges70 bronze badges asked Nov 21, 2024 at 21:03 user6781user6781 2912 silver badges14 bronze badges2 Answers
Reset to default 3The reason what you are trying won't work is because input[type=checkbox]
is part of the selector for the directive CheckboxControlValueAccessor. By trying making the type attribute dynamic, you are preventing the directive from being used, since directive selectors are found at compile time and therefore only work with static values.
One way to accomplish what you're trying to do is with a @switch block where every expected input type is handled.
@switch(file.type) {
@case('checkbox') {
<input type="checkbox" [formControlName]="field.formControlName" />
}
@default {
<input type="text" [formControlName]="field.formControlName" />
}
}
Well, very minor mistake my friend!
<div class="form-floating mb-3">
<!-- Just add checked property below -->
<input type="{{field.type}}" formControlName="{{field.formControlName}}" [checked]="field.value" />
<label class="form-label">
{{field.label}}
</label>
</div>
Hope that helps!
Added StackBlitz demo for reference.
本文标签: angularGetting form39s input type from TypeScriptStack Overflow
版权声明:本文标题:angular - Getting form's input type from TypeScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736307162a1933214.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论