在angular 6 form builder中找不到具有未指定名称属性的控件

在angular 6 form builder中找不到具有未指定名称属性的控件,angular,angular6,angular-forms,Angular,Angular6,Angular Forms,我在尝试用HTML文件更新网格表时收到此错误消息 在这里,我使用静态数据来显示表,并从显示预处理表的其他组件导入,我添加了一个更新按钮,该按钮具有重定向到另一个页面以更新数据的功能 问题出现在HTML文件的第一行,即:;[formGroup]=“我的车辆” 我曾尝试使用不同的表单组名称进行检查,但问题仍然是相同的 从'@angular/core'导入{Component,OnInit}; 从'@angular/Router'导入{Router,ActivatedRoute,Params}; 从

我在尝试用HTML文件更新网格表时收到此错误消息

在这里,我使用静态数据来显示表,并从显示预处理表的其他组件导入,我添加了一个更新按钮,该按钮具有重定向到另一个页面以更新数据的功能

问题出现在HTML文件的第一行,即:;[formGroup]=“我的车辆”

我曾尝试使用不同的表单组名称进行检查,但问题仍然是相同的

从'@angular/core'导入{Component,OnInit};
从'@angular/Router'导入{Router,ActivatedRoute,Params};
从'@angular/forms'导入{FormBuilder,FormGroup};
@组成部分({
选择器:“应用程序积垢”,
templateUrl:“./crud.component.html”,
})
导出类CrudComponent实现OnInit{
我的载体:FormGroup;
显示:布尔;
id:编号;
vin:任何;
年份:数字;
品牌:弦;
颜色:字符串;
车辆:任何车辆;
资料:有;
构造函数(私有activatedRoute:activatedRoute,私有fb:FormBuilder){
}
恩戈尼尼特(){
this.myvehicle=this.fb.group({
vin:[''],
年份:[''],
品牌:[''],
颜色:['']
});
这辆车=[
{
id:1,vin:'dsad231ff',年份:2012,品牌:'VW',颜色:'橙色'
},
{
id:2,vin:GWREGE345,年份:2011,品牌:奥迪,颜色:黑色
},
{
id:3,vin:'h354htr',年份:2005,品牌:'Renault',颜色:'Gray'
},
{
id:4,vin:'j6w54qgh',年份:2003,品牌:'BMW',颜色:'Blue'
},
{
id:5,vin:'hrtwy34',年份:1995,品牌:'Mercedes',颜色:'Orange'
}
];
调试器
this.activatedRoute.paramMap
.订阅(参数=>{
this.id=+params.get('id');
});
this.vehicle.forEach(元素=>{
if(element.id==this.id){
这个。数据=元素;
}
});
this.myvehicle.patchValue({
vin:this.Data.vin,
年份:this.Data.year,
品牌:this.Data.brand,
颜色:this.Data.color
});
}
}

Vin:


年份:

品牌:

颜色:


我认为主要问题在这里

<input type="text" [formControlName]="vin" ><br><br>
作为控件名,可以通过以下方式进行修复:

<input type="text" formControlName="vin" ><br><br>
可以简化

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

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

此处,patchValue仅在您在router params中拥有正确的id时运行

我已将您的描述性文本从代码段中删除,并放入问题的主体,以便更容易阅读。我还做了一些小的语法改进谢谢Nick顺便问一句,你知道问题到底是什么吗?“this.activatedRoute.paramMap.subscribe(params=>{const id=params.get('id');if(this.vehicle[id]){this.myvehicle.patchValue(this.vehicle[id]);}”;单击“更新”时,此值显示下一行的值
  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]);
    }
  });