给定错误的类型脚本必须提供formcontrol名称id的值 [英] type script given error must supply value for formcontrol name id

查看:109
本文介绍了给定错误的类型脚本必须提供formcontrol名称id的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误:错误错误:必须为名称为'id'的表单控件提供一个值.即使我已经提供了相同的价值,这是我的打字稿代码

I have been getting the following error: ERROR Error: Must supply a value for form control with name: 'id'. even though i have provided value for the same and here is my typescript code

import { Component, OnInit,Inject } from '@angular/core';
import {MatDialog,MatDialogRef,MAT_DIALOG_DATA} from '@angular/material';
import {FormGroup,FormBuilder,Validators} from '@angular/forms';
import {AdminService} from '../../services/admin.service';
@Component({
  selector: 'app-subcategory-modal',
  templateUrl: './subcategory-modal.component.html',
  styleUrls: ['./subcategory-modal.component.css']
})
export class SubcategoryModalComponent implements OnInit {
   subcategoryForm:FormGroup;
   category:any;
  constructor(public subCategoryDialogref:MatDialogRef<SubcategoryModalComponent>,@Inject(MAT_DIALOG_DATA)public data:string,private formBuilder:FormBuilder,private service:AdminService)
   {
      this.generateCategory();
   }

  ngOnInit() {
    this.createForm();
    this.generateSubcategory(this.data);

  }
   createForm()
   {
     this.subcategoryForm=this.formBuilder.group({
       id:[null,Validators.required],
       subcategoryName:[null,Validators.required],
       category:[null,Validators.required]
     });
   }
   generateSubcategory(data)
   {

     this.service.getSubcategorys(data).subscribe(res=>{
         console.log(res.result);
     this.subcategoryForm.setValue({
       id:res.result.Name
     });
     },err=>{

     });
   }
   generateCategory()
   {
     this.service.getCategory().subscribe(res=>{
       this.category=res;
     });
   }
}

这是我的html代码:-

and this is my html code :-

<form [formGroup]="subcategoryForm">
  <div class="form-group">
    <input type="text" class="form-control" formControlName="id"value="" name="id">
    </div>
  <div class="form-group">
    <input type="text" class="form-control" formControlName="subcategoryName" name="subcategoryName" >
  </div>
  <div class="form-group">
  <select class="form-control" name="category" formControlName="category">
      <option value="0">-Select-</option>
      <option  *ngFor="let data of category?.result" value="{{data.id}}">{{data.Name}}</option>
    </select>
  </div>
  <div class="form-group text-center">
    <button type="button" class="btn btn-primary" name="button">Update</button>
  </div>
</form>

有人可以告诉我我要去哪里错吗?以及为什么这个错误不断发生?

can anybody tell me where i'm going wrong? and why this error keeps happening?

推荐答案

由于只想更改控件 id 的值,因此应使用 patchValue 代替 setValue 方法可修改该formControl的值.

Since you want to change only the value of the control id, you should use patchValue instead of setValue method to modify the value of that formControl.

this.subcategoryForm.patchValue({
       id:res.result.Name
});

如果要使用 setValue 方法,则可以从formControl id 调用它:

if you want to use setValue method, you can call it from the formControl id :

this.subcategoryForm.controls['id'].setValue(res.result.Name);

这篇关于给定错误的类型脚本必须提供formcontrol名称id的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆