Angular 2: 迭代对喷射形式控制的元素

我想要
markAsDirty

所有控制在里面
FormGroup

.
已邀请:

冰洋

赞同来自:

发现了
Object.keys

可以应对它..


Object.keys/this.form.controls/.forEach/key => {
this.form.get/key/.markAsDirty//;
}/;


为了 Angular 8+ 使用以下内容 /基于答案Michelangelo/:


Object.keys/this.form.controls/.forEach/key => {
this.form.controls[key].markAsDirty//;
}/;

龙天

赞同来自:

就像它一样,没有必要使用魔法就可以做到这一点

Object.keys/.../

:


for /const field in this.form.controls/ { // 'field' is a string

const control = this.form.get/field/; // 'control' is a FormControl

}

江南孤鹜

赞同来自:

收到的答案是忠于平面形式的结构,但并没有完全回应原始问题。 网页可能需要嵌套 FormGroups 和 FormArrays, 我们必须考虑到这一点以创建可靠的解决方案。


public markControlsDirty/group: FormGroup | FormArray/: void {
Object.keys/group.controls/.forEach//key: string/ => {
const abstractControl = group.controls[key];

if /abstractControl instanceof FormGroup || abstractControl instanceof FormArray/ {
this.markControlsDirty/abstractControl/;
} else {
abstractControl.markAsDirty//;
}
}/;
}

八刀丁二

赞同来自:

它看起来像一个函数
get

不再有效地提取表单中的某些值 Angular 8, 这就是我根据答案决定它的方式 @Liviu Ilea.


for /const field in this.myForm.controls/ { // 'field' is a string
console.log/this.myForm.controls[field].value/;
}

帅驴

赞同来自:

使用 @Marcos answer, 我创建了一个可以通过传递调用的函数 formGroup 作为参数,它标记每个控件 formGroup children 脏,只是为了使其适合从代码周围的更多地区使用,例如,在服务中。


public touchAllFormFields/formGroup: FormGroup/: void {
Object.keys/formGroup.controls/.forEach//key/ => {
formGroup.get/key/.markAsDirty//;
}/;
}


我希望这个能帮上忙 ;/

诸葛浮云

赞同来自:

Object.keys/ this.registerForm.controls/.forEach/key => {
this.registerForm.controls[key].markAsDirty//;
}/;

卫东

赞同来自:

这就是对我有用的东西


private markFormGroupTouched/formGroup: FormGroup/ {
Object.keys/formGroup.controls/.forEach//key/ => {
const control = formGroup.controls[key];
control.markAsDirty//;
if //control instanceof FormGroup// {
this.markFormGroupTouched/control/;
}
}/;
}

三叔

赞同来自:

我创建了这个功能来制作 it*
我有控制命名 'order', 我给了他一个指数。


{"conditionGroups": [
{
"order": null,
"conditions": []
}
]
}


updateFormData// {
const control = <formarray>this.form.controls['conditionGroups'];
control.value.map//x,index/=&gt;{
x.order = index;
}/


</formarray>

要回复问题请先登录注册