Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 使用formgroup中的值更新模型_Angular_Forms_Angular7 - Fatal编程技术网

Angular 使用formgroup中的值更新模型

Angular 使用formgroup中的值更新模型,angular,forms,angular7,Angular,Forms,Angular7,我有一个formgroup,我用模型中的数据填充它。例如: inputForm: FormGroup; ngOnInit() { this.inputForm = this.formBuilder.group({ name: [this.myObject.name, Validators.required] }, {updateOn: 'submit'}); } 这将在输入字段中显示myObject.name,这就是我想要的。然后键入新值并提交时,模型不会使

我有一个formgroup,我用模型中的数据填充它。例如:

inputForm: FormGroup;

ngOnInit() {
    this.inputForm = this.formBuilder.group({
        name: [this.myObject.name, Validators.required]
    }, {updateOn: 'submit'});
 }

这将在输入字段中显示myObject.name,这就是我想要的。然后键入新值并提交时,模型不会使用新值更新。我知道这是预期的行为,但有没有办法将输入值绑定到myObject.name,以便它自动更新,还是必须手动从formgroup中检索新值并分配它?

formBuilder不接受{updateOn},您必须直接使用newFormGroup并创建新的FormControl

this.inputForm = new FormGroup({
        name: new FormControl(this.myObject.name, Validators.required)
    }, {updateOn: 'submit'});
(我个人更喜欢这种方式,而不是使用formBuilder)

但这不会更新对象,它只是更新inputForm。要更新对象,您需要以下内容:

this.myObject=this.inputForm.value

formBuilder不接受{updateOn},您必须直接使用newFormGroup并创建新的FormControl

this.inputForm = new FormGroup({
        name: new FormControl(this.myObject.name, Validators.required)
    }, {updateOn: 'submit'});
(我个人更喜欢这种方式,而不是使用formBuilder)

但这不会更新对象,它只是更新inputForm。要更新对象,您需要以下内容:

this.myObject=this.inputForm.value