在Angular 6表单构建器中找不到未指定名称属性的控件。

3

在尝试通过 HTML 文件更新网格表格时,我遇到了这个错误信息。

我使用静态数据来显示表格,并从其他组件导入 primeng 表格,然后添加了一个“更新”按钮和一个函数,该函数重定向到另一个页面以进行数据更新。

问题出现在 HTML 文件的第一行中,即 [formGroup]="myvehicle"。

我尝试更改不同的表单组名称,但问题仍然存在。

import { Component, OnInit } from '@angular/core';
import {Router, ActivatedRoute, Params} from '@angular/router';
import { FormBuilder, FormGroup } from '@angular/forms';

@Component({
  selector: 'app-crud',
  templateUrl: './crud.component.html',
})
export class CrudComponent implements OnInit {
  myvehicle: FormGroup;
  display: boolean;
  id: number;
  vin: any;
  year: number;
  brand: string;
  color: string;
  vehicle: any;
  Data: any;

  constructor(private activatedRoute: ActivatedRoute, private fb: FormBuilder) {
   }

  ngOnInit() {
    this.myvehicle = this.fb.group({
    vin: [''],
    year: [''],
    brand: [''],
    color: ['']
  });
    this.vehicle = [
      {
       id: 1 , vin: 'dsad231ff' , year : 2012 , brand: 'VW' , color: 'Orange'
      },
      {
        id: 2 , vin: 'gwregre345' , year : 2011 , brand: 'Audi' , color: 'Black'
      },
      {
        id: 3 , vin: 'h354htr' , year : 2005 , brand: 'Renault' , color: 'Gray'
      },
      {
        id: 4, vin: 'j6w54qgh' , year : 2003 , brand: 'BMW', color: 'Blue'
      },
      {
        id: 5, vin: 'hrtwy34' , year : 1995 , brand: 'Mercedes' , color: 'Orange'
      }
    ];
    debugger
    this.activatedRoute.paramMap
    .subscribe( params => {
    this.id = +params.get('id');
    });

      this.vehicle.forEach(element => {
        if (element.id === this.id) {
            this.Data = element;
              }
      });
      this.myvehicle.patchValue({
        vin: this.Data.vin,
        year: this.Data.year,
        brand: this.Data.brand,
        color:  this.Data.color
      });
  }

}
<form [formGroup]="myvehicle">
<label >Vin:</label>
<input type="text" [formControlName]="vin" ><br><br>
<label >Year:</label>
<input type="text" [formControlName]="year" ><br><br>
<label >Brand:</label>
<input type="text" [formControlName]="brand" ><br><br>
<label >Color:</label>
<input type="text" [formControlName]="color" ><br><br>
</form>


我已经将您的描述性文本从代码片段中移除,并放入问题的主体中,以便更容易阅读。我还进行了一些小的语法改进。 - Nick
谢谢Nick,顺便问一下你知道问题具体是什么吗? - anirudh talluri
1个回答

6

我认为主要问题在这里

<input type="text" [formControlName]="vin" ><br><br>

您试图传递一个空变量

vin: any;

作为控件名称,可以这样固定:
<input type="text" formControlName="vin" ><br><br>

只需要移除方括号并将控件名称设置为字符串即可解决您的问题。

此外,这部分代码也需要修改。

  this.activatedRoute.paramMap
.subscribe( params => {
this.id = +params.get('id');
});

  this.vehicle.forEach(element => {
    if (element.id === this.id) {
        this.Data = element;
          }
  });
  this.myvehicle.patchValue({
    vin: this.Data.vin,
    year: this.Data.year,
    brand: this.Data.brand,
    color:  this.Data.color
  });

可以简化

    this.activatedRoute.paramMap
  .subscribe(params => {
    const id = params.get('id');

    if (this.vehicle[id]) {
      this.myvehicle.patchValue(this.vehicle[id]);
    }
  });

只有在路由参数中有正确的id时,这里的patchValue才会运行。


this.activatedRoute.paramMap.subscribe(params => { const id = params.get('id');if (this.vehicle[id]) { this.myvehicle.patchValue(this.vehicle[id]); } });当单击更新时,此值显示下一行的值 - anirudh talluri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接