0

I have a formgroup like below:

 this.myform = this.fb.group({
      mydata: ''
    });

I was able to set the data with formgroup setValue but in rendering the values in HTML, the option was not selected. This is the HTML:

   <div *ngFor="let item of (items$ | async); index as i; first as first">
        <input type="radio" id="{{item.itemId}}" name="test" value="{{item.itemId}}"  [formControl]="myform.controls.mydata" 
    selected = "(items$ | async).length ===1">
</div>

This doesnot select the first input label in HTML but in typescript the form is valid .

3
  • 1
    Have you tried anything so far? Commented Sep 10, 2018 at 10:34
  • Just add selected on your formControlName="mydata". Everytime you istance your form is set to valid just look this: stackoverflow.com/questions/44508982/… Commented Sep 10, 2018 at 10:39
  • is it possible to do this dynamically like selected = "{{item.length >1}}" It doesnot really work for me Commented Sep 11, 2018 at 4:06

2 Answers 2

1

Use the setValue() method to set a new value for an individual control. The setValue() method strictly adheres to the structure of the form group and replaces the entire value for the control.

this.myform.setValue({ mydata: 'yourData' });
Sign up to request clarification or add additional context in comments.

1 Comment

This doesnot update the HTML. I cannot see the radio button beign selected
1

use the patchValue to set the value for the whole form

 this.myform .patchValue({ mydata: 'yourData' });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.