gpt4 book ai didi

模板中的 angular2 和 formControlName 值

转载 作者:太空狗 更新时间:2023-10-29 16:57:59 26 4
gpt4 key购买 nike

哦,angular2...为什么这么难?

<input type="text" formControlName="exposure" type="hidden">
<label>{{exposure}}</label>

如果我在输入中使用 formControlName,则值是正确的。

如何获取模板中的曝光值?标签中的空白

最佳答案

The formControlName directive is designed to be used with a parent FormGroupDirective (selector: [formGroup]).

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 属性:

export class MyComponent {
form = new FormGroup({
exposure: new FormControl('')
});

get exposure(): FormControl { return this.form.get('exposure'); }

然后,在您的组件模板中,您可以引用exposure:

<input type="text" formControlName="exposure" type="hidden">
<label>{{exposure.value}}</label>

关于模板中的 angular2 和 formControlName 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42732375/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com