How to set value to form control in Reactive Forms in Angular

前端 未结 6 1376
广开言路
广开言路 2020-11-28 08:15

Hi Everyone I\'m new to angular. Actually, I\'m trying to subscribe data from a service and that data, I\'m passing to form control of mine from (example, it\'s like an edit

6条回答
  •  执笔经年
    2020-11-28 08:47

    Try this.

    editqueForm =  this.fb.group({
       user: [this.question.user],
       questioning: [this.question.questioning, Validators.required],
       questionType: [this.question.questionType, Validators.required],
       options: new FormArray([])
    })
    

    setValue and patchValue

    if you want to set the value of one control, this will not work, therefor you have to set the value of both controls:

    formgroup.setValue({name: ‘abc’, age: ‘25’}); It is necessary to mention all the controls inside the method. If this is not done, it will throw an error.

    On the other hand patchvalue is a lot easier on that part, let’s say you only want to assign the name as a new value:

    formgroup.patchValue({name:’abc’});

提交回复
热议问题