formGroup 期望一个 FormGroup 实例,动态表单

huangapple go评论74阅读模式
英文:

formGroup expects a FormGroup instance, dynamic form

问题

I have translated the code portions for you:

<!-- language: lang-html -->
<h2 mat-dialog-title>Title</h2>
<mat-dialog-content [formGroup]="formGroup">
    <ng-container class="form-row" *ngFor="let item of intData">
        <app-input-field [formGroup]="formGroup"
                        [data]="item.data" 
                        [controlName]="item.controlName" 
                        [placeHolder]="item.placeHolder" 
                        *ngIf="item.input==='inputField'">
        </app-input-field>
    </ng-container>
</mat-dialog-content>
<mat-dialog-actions>
    <button class="mat-raised-button mat-primary" [mat-dialog-close]="data" (click)="save()">Publish</button>
</mat-dialog-actions>
<!-- language: lang-ts -->
@Component({
  selector: 'app-modal-admin-cops',
  templateUrl: './modal-admin-cops.component.html',
  styleUrls: ['./modal-admin-cops.component.css']
})
export class ModalAdminCopsComponent implements AfterViewInit {
  private controllerNames: string[] = [];
  private intData;
  private formGroup: FormGroup;
  
  constructor(
    private renderer: Renderer2,
    private http: HttpClient,
    private formBuilder: FormBuilder,
    private dialogRef: MatDialogRef<ModalAdminCopsComponent>,
    @Inject(MAT_DIALOG_DATA) public data: any[]
  ) {
    this.intData = data;
    this.cargar();
  }

  cargar() {
    this.intData.forEach(x => {
      console.log(x.data);
      if (x.input === "inputField") {
        this.controllerNames.push(x.controlName);
      }
    });
    
    console.log(this.controllerNames);
  }

  ngAfterViewInit() {
    console.log("llega");
    this.formGroup = this.toFormGroup();
    console.log(this.formGroup);
  }

  toFormGroup(): FormGroup {
    let group: any = {};
    this.controllerNames.forEach(x => {
      group[x] = new FormControl();
    });
    return new FormGroup(group);
  }

  save() {
    location.reload();
  }
}
<!-- language: lang-ts -->
@NgModule({
  declarations: [
    // ...
  ],
  imports: [
    // ...
  ],
  entryComponents: [
    ModalAdminCopsComponent // Add your dialog component here
  ]
})
export class YourModule { }

Please note that you should replace "YourModule" with the actual name of your module where you declare your dialog component.

英文:

I'm making a dynamic form using this guide but I'm a little bit confused, the form is in a dialog made with angular materials.

modal-admin-cops.component.html

<!-- language: lang-html -->

&lt;h2 mat-dialog-title&gt;Title&lt;/h2&gt;
&lt;mat-dialog-content [formGroup]=&quot;formGroup&quot;&gt;
        &lt;ng-container class=&quot;form-row&quot; *ngFor=&quot;let item of intData&quot;&gt;
            &lt;app-input-field [formGroup]=&quot;formGroup&quot;
                            [data]=&quot;item.data&quot; 
                            [controlName]=&quot;item.controlName&quot; 
                            [placeHolder]=&quot;item.placeHolder&quot; 
                            *ngIf=&quot;item.input===&#39;inputField&#39;&quot;&gt;
            &lt;/app-input-field&gt;
            &lt;!--
            &lt;app-file-field [form]=&quot;form&quot;
                        [data]=&quot;item&quot; 
                        [controlName]=&quot;item.controlName&quot; 
                        [placeHolder]=&quot;item.placeHolder&quot; 
                        *ngIf=&quot;item.input===&#39;fileField&#39;&quot;&gt;

            &lt;/app-file-field&gt;
            &lt;app-select-field [form]=&quot;form&quot;
                            [data]=&quot;item&quot; 
                            [controlName]=&quot;item.controlName&quot; 
                            [placeHolder]=&quot;item.placeHolder&quot; 
                            *ngIf=&quot;item.input===&#39;selectField&#39;&quot;&gt;
            &lt;/app-select-field&gt;
            --&gt;
        &lt;/ng-container&gt;
&lt;/mat-dialog-content&gt;
&lt;mat-dialog-actions&gt;
    &lt;button class=&quot;mat-raised-button mat-primary&quot; [mat-dialog-close]=&quot;data&quot; (click)=&quot;save()&quot;&gt;Publish&lt;/button&gt;
&lt;/mat-dialog-actions&gt;

modal-admin-cops.component.ts

<!-- language: lang-ts -->

@Component({
  selector: &#39;app-modal-admin-cops&#39;,
  templateUrl: &#39;./modal-admin-cops.component.html&#39;,
  styleUrls: [&#39;./modal-admin-cops.component.css&#39;]
})
export class ModalAdminCopsComponent implements AfterViewInit {
  private controllerNames:string[]=[];
  private intData;
  private formGroup:FormGroup;
  
  constructor(
    private renderer:Renderer2,
    private http:HttpClient,
    private formBuilder: FormBuilder,
    private dialogRef:MatDialogRef&lt;ModalAdminCopsComponent&gt;,
    @Inject(MAT_DIALOG_DATA) public data:any[]) 
    {
      this.intData=data;
      this.cargar();
    }
  cargar(){
    this.intData.forEach(x=&gt;{
      console.log(x.data);
      if(x.input===&quot;inputField&quot;){
        this.controllerNames.push(x.controlName);
      }
    });
    
    console.log(this.controllerNames);
  }
  ngAfterViewInit(){
    console.log(&quot;llega&quot;);
    this.formGroup=this.toFormGroup();
    console.log(this.formGroup);
  }
  toFormGroup():FormGroup{
    let group:any;
    this.controllerNames.forEach(x=&gt;{
      group[x]=new FormControl();
    });
    return new FormGroup(group);
  }
  save(){
    location.reload();
  }
}

The @Inject(MAT_DIALOG_DATA) public data:any[]) receives something like this:

<!-- language: lang-ts -->

[{
   input:&quot;inputField&quot;,
   controlName:&quot;nombre&quot;,
   placeHolder:&quot;Escribe el nombre de la cop&quot;,
   data:{
     cod:here goes a number,
     desc:Here goes a string
  }
}]

Sometimes data maybe an array of {cod,desc}

input-field.component.html

<!-- language: lang-html -->

&lt;div [formGroup]=&quot;formGroup&quot;&gt;
        &lt;mat-form-field #input class=&quot;expand&quot; [class]=&quot;placeHolder&quot;&gt;
                &lt;input matInput
                        [(value)]=&quot;data.desc&quot;
                        [placeholder]=&#39;placeHolder&#39; 
                        [formControlName]=&#39;controlName&#39;&gt;
        &lt;/mat-form-field&gt;
&lt;/div&gt;

input-field.component.ts

<!-- language: lang-ts -->

@Component({
  selector: &#39;app-input-field&#39;,
  templateUrl: &#39;./input-field.component.html&#39;,
  styleUrls: [&#39;./input-field.component.css&#39;]
})
export class InputFieldComponent implements AfterViewInit {
  @Input() formGroup:FormGroup;
  @Input() data:any;
  @Input() controlName:string;
  @Input() placeHolder:string;
  
  constructor() {
    
  }
  
}

When I click a button that start the dialog I get this:

ModalAdminCopsComponent.html:2 ERROR Error: formGroup expects a FormGroup instance. Please pass one in.
InputFieldComponent.html:1 ERROR Error: formGroup expects a FormGroup instance. Please pass one in.

What I am trying to do is to create the form dynamically with different types of inputs.

Edit1:
If i used:

<!-- language: lang-ts -->

ngOnInit(){
  this.formGroup=this.toFormGroup();
  console.log(this.formGroup);
}

I get:

formGroup expects a FormGroup instance. Please pass one in.

If I used:

<!-- language: lang-ts -->

ngAfterViewInit(){
  this.formGroup=this.toFormGroup();
  console.log(this.formGroup);
}

i get:

Cannot read property &#39;get&#39; of undefined
Cannot set property &#39;nombre&#39; of undefined

答案1

得分: 0

在构造函数或ngInit中初始化您的formGroup。

this.formGroup = this.formBuilder.group({
  name: new FormControl('')
});
英文:

Initialize your formGroup in the constructor or in ngInit

this.formGroup = this.formBuilder.group({
  name: new FormControl(&#39;&#39;)
});

答案2

得分: 0

我把这段代码放在input.component.ts文件中:

ngOnInit(){
  this.formGroup.controls[this.controlName]=new FormControl();
}

似乎是将其添加到了formGroup中。

英文:

Finally a get a way to add the controllers:
I put this in the input.component.ts

<!-- language: lang-ts -->

ngOnInit(){
  this.formGroup.controls[this.controlName]=new FormControl();
}

That seems to add to the formGroup.

huangapple
  • 本文由 发表于 2020年1月3日 18:00:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576496.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定