简体   繁体   中英

How to Validate Radio Buttons Field in angular with reactive Forms

I have name and values ,calling from the ts side and want to validate it.the values are'nt getting from the event and it's been comming such as (on) .

<form [formGroup]="reactiveForm">
  <div *ngFor="let item of checkBoxValueList">
    <input 
    type="radio" 
    formControlName="yourChoice"
    [value]="item"
    (change)="event($event)"
    >
    {{item}}
  </div>
 </form>
 <pre>
 {{reactiveForm.value | json}}
</pre>
   checkBoxValueList = [
    'Reading',
    'Watching',
    'Traveling',
    'Cooking'
  ];
  reactiveForm: FormGroup = new FormGroup({
    yourChoice: new FormControl()
  });

  constructor() {}

  edit(eve) {
    console.log(eve);
    console.log("target", eve.target.value);
  }

You can pass directly your value instead of event. Try it.

(change)="edit(item)".

Hope this helps.

I guess that you only need the selected value on change event and validate it. So here you can access the selected value of radio button by our reactiveForm variable.

event(eve) {
 console.log(eve);
 // console.log("target", eve.target.value);
 console.log(this.reactiveForm.value['yourChoice']); //this will give your selected value
}

Hope this helps!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM