【问题标题】:Must supply a value for name in form in Angular必须以 Angular 的形式提供名称的值
【发布时间】:2019-09-04 11:43:48
【问题描述】:

我在 Anuglar 8 中有一个表格,表格中有一个下拉列表,用户可以在其中选择一个选项。但是当第一次加载表单时,它说表单控件的值必须提供一个值

所以这是下拉列表:


  <mat-form-field>
        <mat-select name="notificationType"  [(ngModel)]="someVariable" #notificationType="ngModel" placeholder="Notification type"
          placeholder-i18n required>
          <mat-option value="" disabled></mat-option>
          <mat-option [value]="notificationTypes.Never" i18n>Never</mat-option>
          <mat-option [value]="notificationTypes.OnCreation" i18n>OnCreation</mat-option>
          <mat-option [value]="notificationTypes.Scheduled" i18n>Scheduled</mat-option>
        </mat-select>
        <mat-error>
          <app-element-edit-field-error [errors]="notificationType.errors"></app-element-edit-field-error>
        </mat-error>
      </mat-form-field>

这是 ngOniit 函数:

ngOnInit() {

    // Needed to avoid the "there are no controls associated with this form" error.

    setTimeout(() => {
      if (!this.editable) {
        for (const key of _.keys(this.definitionHeaderForm.controls)) {
          this.definitionHeaderForm.controls[key].disable();
        }
      }   

      if (this.notificationTypes === undefined) {
        this.notificationTypes = null;
      } else {
        EcheqUtils.setFormValue(this.definitionHeaderForm, this.definition);
        EcheqUtils.applyErrors(this.definitionHeaderForm, this.errorTree);
      }



      this.definitionHeaderForm.valueChanges.subscribe(value => {
        for (const field of _.keys(this.definitionHeaderForm.controls)) {
          this.definition[field] = value[field];

          //const hallo2 = this.definitionHeaderForm.controls['notificationType'].setValue(this.notificationTypes.Never);
          //console.log(hallo2);
        }
      });
      this.definitionHeaderForm.statusChanges.subscribe(status => {
        this.canClose = status === "VALID" || status === "DISABLED";
      });
    }, 1000);
  }

以此类推:

 EcheqUtils.setFormValue(this.definitionHeaderForm, this.definition);

我收到此错误:

core.js:12584 ERROR Error: Must supply a value for form control with name: 'notificationType'.
    at forms.js:3370
    at forms.js:3309
    at Array.forEach (<anonymous>)
    at FormGroup.push../node_modules/@angular/forms/fesm5/forms.js.FormGroup._forEachChild (forms.js:3309)
    at FormGroup.push../node_modules/@angular/forms/fesm5/forms.js.FormGroup._checkAllValuesPresent (forms.js:3368)
    at FormGroup.push../node_modules/@angular/forms/fesm5/forms.js.FormGroup.setValue (forms.js:3158)
    at NgForm.push../node_modules/@angular/forms/fesm5/forms.js.NgForm.setValue (forms.js:3900)
    at Function.push../src/app/echeq-definition/echeq-utils.ts.EcheqUtils.setFormValue (echeq-utils.ts:97)
    at definition-form-dialog.component.ts:73
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)

而这个函数:setFormValue 看起来像这样:

static setFormValue(form: NgForm, object: {}) {

    const copy = {};
    console.log(copy);
    for (const key of _.keys(form.controls)) {
      copy[key] = object[key];
    }
    form.setValue(copy);
  }

谢谢

这样就不会再显示错误了

console.log(copy) 的结果如下所示:

actions: []
awardedVPoints: 0
calculatedVariables: []
initialVariables: []
notificationType: undefined
title: "Nieuwe echeq"
validDays: null
__proto__: Object

这是 ts 文件中的属性:

someVariable: any; 


@ViewChild(NgForm) definitionHeaderForm: NgForm;

【问题讨论】:

  • 能否请您提供definitionHeaderForm 变量是如何定义的?如果您使用 Angular Reactive Forms 和表单组,我也很确定您应该使用 formControlName
  • 您好,埃德里克,感谢您的回复。是模板驱动方式:@ViewChild(NgForm) definitionHeaderForm: NgForm;
  • 如果有必要我也可以给你看整个ts文件

标签: javascript angular forms angular-material undefined


【解决方案1】:

如果您不想为所有表单控件提供值,可以使用patchValue 方法

static setFormValue(form: NgForm, object: {}) {

    const copy = {};
    console.log(copy);
    for (const key of _.keys(form.controls)) {
      copy[key] = object[key];
    }
    form.patchValue(copy); // ?
  }

【讨论】:

  • 哦,是的,但是我没有在方法上使用patchVlaue!所以谢谢!!!你真的拯救了我的一天!它有效。
  • @mightycodeNewton 欢迎您,乐于助人?
  • 马尔巴马维:问题。也许你对这个问题有一个想法:stackoverflow.com/questions/57708641/…
猜你喜欢
  • 2019-01-11
  • 1970-01-01
  • 2018-11-02
  • 2018-12-05
  • 1970-01-01
  • 2018-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多