找不到带有角形路径的控制

时间:2018-09-27 15:18:29

标签: javascript angular forms typescript angular4-forms

我正在使用angular 6应用程序并使用动态表单。

app.component.html:

   <form (ngSubmit)="onSubmit()" [formGroup]="form">
    <div formArrayName="multiDynamic">
      <!-- row -->
      <div *ngFor="let record of form.controls.multiDynamic.controls; let i=index">
            <div class="row" [formGroupName]="i">
                <div class="col-md-6" *ngFor="let question of model.questions">
                    <div [ngSwitch]="question.controlType">
                        <div class="form-group form-black label-floating">
                            <label class="control-label">{{question.text | titlecase}} </label>
                            <div *ngSwitchCase="'textbox'"><input type="text" class="form-control" [formControlName]="question.key"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
      </div>
    <button type="submit> Submit </button>
  </form>

app.component.ts:     从'@ angular / core'导入{Component,OnInit,Input};     从'@ angular / forms'导入{FormBuilder,FormGroup,Validators,FormControl,FormArray};

export class AppComponent implements OnInit {
 @Input() model: any;
form: FormGroup;

ngOnInit() {

this.sharedService.getData().subscribe(data => {
  this.items = data;
  console.log(this.items);
});

this.form = this.fb.group({
        multiDynamic: this.fb.array([
          this.model.toGroup()
        ])
      });
  }

}

console.log(model.questions)给出以下内容(扩展数组)

0:Textbox {controlType: "textbox", key: "url-links", text: "url-links"}
1:Textbox {controlType: "textbox", key: "//input[@id='rcmloginuser']", text: "//input[@id='rcmloginuser']"}
2:Textbox {controlType: "textbox", key: "//input[@id='rcmloginpwd']", text: "//input[@id='rcmloginpwd']"}
3:Textbox {controlType: "textbox", key: "//input[@id='rcmloginsubmit'] ", text: "//input[@id='rcmloginsubmit'] "}

我正在使用上面的html和ts文件,如果我给出这一行,

<input id="i" type="text" class="form-control" [formControlName]="question.text">

我遇到了错误, 无法找到路径为“ multiDynamic-> 0->网址链接”的控件

在标签中,

<label class="control-label"> {{question.text | titlecase}} </label>

正在显示文本值,但是如果我在输入框中输入formControlName,那么我将收到此错误。

我正在使用angular的动态形式。

this.model.toGroup()具有以下内容,

export class DynamicFormMultiModel {
    questions = [];
multiDataResponse: any = [];
form: FormGroup;

toGroup() {
    const group: any = {};
    this.questions.forEach((question) => {
        group[question.key] = new FormControl('');
    });

    return new FormGroup(group);
}

    patchData() {
        const arrayGroup: any = {};
        const dataRes: any = [];
        const resObj: any = {};

        // this.multiDataResponse.forEach((element, index) => {
        //     this.questions.forEach((question, inx) => {
        //             arrayGroup[question.key] = new FormControl(element[question.key]);
        //     });
        // });
        resObj['data'] = this.multiDataResponse;

    // return new FormGroup(arrayGroup);
    return resObj;
    }
}

由于我有嵌套代码,因此无法发布整个代码,但是我正在获取数据及其在标签中的显示,但是输入标签抛出错误。 (两者都包含相同的值question.text)。

随附该场景的屏幕截图。

enter image description here

请帮助我修复卡在其中的时间过长。.

0 个答案:

没有答案