Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 无法将初始表单值设置为FormArray_Angular_Typescript_Angular2 Forms - Fatal编程技术网

Angular 无法将初始表单值设置为FormArray

Angular 无法将初始表单值设置为FormArray,angular,typescript,angular2-forms,Angular,Typescript,Angular2 Forms,我有一个被动表单,在cancel上必须再次将初始表单值设置到表单组中 import { Map } from "immutable"; @Input() data: any; public ngOnInit() { if (this.data && this.data.controls) { this.group = this.fb.group({ isActive: [this.data.isActive],

我有一个被动表单,在
cancel
上必须再次将初始表单值设置到表单组中

import { Map } from "immutable";

@Input() data: any;

public ngOnInit() {
    if (this.data && this.data.controls) {
        this.group = this.fb.group({
            isActive: [this.data.isActive],
            items: this.fb.array(this.buildFormArray(this.data.controlPerformers)),
            });

        // Deep copy of the formGroup with ImmutableJs
        this.originalFormData = Map(this.group).toJS();
    }
}

 public buildFormArray(controllers: IControlPerformer[]) {
    return controllers.map((ctlr) => {
        return this.fb.group({
            user: [ctrl.userData],
            ctrlName: [ctlr.name, Validators.required],
            date: [moment(ctlr.date).toDate(), Validators.required],
        });
    });
}

public cancel() {
  const existingItems = this.group.get("items") as FormArray;
  while (existingItems.length) {
            existingItems.removeAt(0);
        }

        // Here the error when trying to set the FormArray value
        this.group.setValue(this.originalFormData.value);  
   }
错误消息:

尚未在此数组中注册任何表单控件。如果您使用的是ngModel,您可能需要检查下一个勾号(例如,使用setTimeout)

这有同样的问题,但我无法在我的情况下解决它

更新-低于
formGroup
的值。它看起来不错,初始化正确

{
 "isActive": true,
 "items": [
  {
   "user": "Walter",
   "ctrlName": "Orders",
   "date": "2018-03-18T23:00:00.000Z"
  }
}

如果从表单数组中删除项,则需要重新添加它们,因为
setValue
patchValue
函数在表单控件缺失时不会创建表单控件,而只会设置/修改现有表单控件值。因此,只需将新控件添加到空
FormArray

public cancel() {
  const existingItems = this.group.get("items") as FormArray;
  while (existingItems.length) {
    existingItems.removeAt(0);
  }

  // Even adding a new FormGroup to the array, the exception remains.
  // existingItems.push(this.fb.group({})););

  // Here the error when trying to set the FormArray value
  this.group.patchValue(this.originalFormData.value);
  this.originalFormData.value.items.forEach(item => {
    existingItems.push(this.fb.group(item)); 
  });
}

STACKBLITZ:

如果从表单数组中删除项,则需要重新添加它们,因为
setValue
patchValue
函数在表单控件缺失时不会创建表单控件,而只会设置/修改现有表单控件值。因此,只需将新控件添加到空
FormArray

public cancel() {
  const existingItems = this.group.get("items") as FormArray;
  while (existingItems.length) {
    existingItems.removeAt(0);
  }

  // Even adding a new FormGroup to the array, the exception remains.
  // existingItems.push(this.fb.group({})););

  // Here the error when trying to set the FormArray value
  this.group.patchValue(this.originalFormData.value);
  this.originalFormData.value.items.forEach(item => {
    existingItems.push(this.fb.group(item)); 
  });
}

STACKBLITZ:

在推进formarray时,我面临一个问题,我需要设置错误,如果该表单数组控件有错误,怎么做?您可以使用
setErrors()
method()在任何抽象控件上设置错误,如
existingItems.setErrors({yourror:true})
。要获取错误,可以使用
getError()
或检查
现有项。错误
objectI在值更改时设置了错误,但在最初从服务器响应创建表单数组时,我可以设置值,但无法设置错误,请您查看一下,您会更清楚地了解我的问题,我在进入表单数组时遇到了一个问题,我需要设置如果该表单数组控件有错误,该如何做您可以使用
setErrors()
method(),如
existingItems.setErrors({yourrerror:true})
在任何抽象控件上设置错误。要获取错误,您可以使用
getError()
或检查
现有项。错误
object我已设置了值更改错误,但在最初从服务器响应创建表单数组时,我可以设置值,但无法设置错误。请您查看一下,您会更清楚地了解我的要求吗