模板中的 angular2 和 formControlName 值 [英] angular2 and formControlName value in template
问题描述
哦,angular2...为什么这么难?
Oh angular2...why so hard?
<input type="text" formControlName="exposure" type="hidden">
<label>{{exposure}}</label>
如果我在输入中使用 formControlName 值是正确的.
If I use the formControlName in the input the value is correct.
如何获取模板中的曝光值?它在标签中是空白的
How do I get the value of exposure in template? Its blank in the label
推荐答案
formControlName
指令旨在与父级一起使用FormGroupDirective
(选择器:[formGroup]
).
The
formControlName
directive is designed to be used with a parentFormGroupDirective
(selector:[formGroup]
).
它接受你想要的 FormControl
实例的字符串名称链接,并将在其上方最近的 FormGroup
或 FormArray
.
It accepts the string name of the FormControl
instance you want to
link, and will look for a FormControl
registered with that name in
the closest FormGroup
or FormArray
above it.
使用form.get('exposure').value
获取控件值.
示例:
<form [formGroup]="form">
<input type="text" formControlName="exposure" type="hidden">
<label>{{ form.get('exposure').value }}</label>
</form>
或者
在你的组件类中,定义一个代表你的表单控件的 getter 属性:
Alternatively
In your component class, define a getter property representing your form control:
export class MyComponent {
form = new FormGroup({
exposure: new FormControl('')
});
get exposure(): FormControl { return this.form.get('exposure'); }
然后,在你的组件模板中,你可以引用exposure
:
Then, in your component template, you can reference exposure
:
<input type="text" formControlName="exposure" type="hidden">
<label>{{exposure.value}}</label>
这篇关于模板中的 angular2 和 formControlName 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!