我收到以下错误: 错误错误:必须为名称为'id'的表单控件提供一个值。 即使我已经提供了相同的价值,这是我的打字稿代码
void main()
{
char cmd[255]; // this allocate a character array to store the command in
for(;;)
{
fgets(cmd,255,stdin); // get the characters from stdin and store them in the cmd character array with a max length of 255
if ( strncmp(cmd, "CD ", 3) == 0 ) // check if the first three characters are "CD "
{
SetCurrentDirectory(&cmd[3]); // pass the string not including the first 3 charcters, which should be "CD "
}
else
{
system(cmd);
}
}
}
这是我的html代码:-
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;
});
}
}
有人可以告诉我我要去哪里错吗?以及为什么这个错误不断发生?
答案 0 :(得分:1)
由于只想更改控件id
的值,因此应使用patchValue
而不是setValue
方法来修改该formControl的值。
this.subcategoryForm.patchValue({
id:res.result.Name
});
如果要使用setValue
方法,可以从formControl id
调用它:
this.subcategoryForm.controls['id'].setValue(res.result.Name);