简体   繁体   中英

unable to bind value to radio button

I am trying to implement a radio group in a list of items. I am unable to set the radion button checked based on the value. Please guide me.

HTML

  <div *ngFor="let item of data; let i = index">
  <nb-radio-group class="p-3 d-inline-flex">
                            <nb-radio [checked]="item.data.v === true">open</nb-radio>
                            <nb-radio [checked]="item.data.v === false">close</nb-radio>
                          </nb-radio-group>
  </div>

only the radio button in last item of an array shows wether the radio button is checked or not. others does not bind data properly.

Please consider using the value property

<nb-radio-group class="p-3 d-inline-flex" [(value)]="item.data.v">
  <nb-radio [value]="true">open</nb-radio>
  <nb-radio [value]="false">close</nb-radio>
</nb-radio-group>

you should use a *ngswitch to display your opion then based on if the user click or not you will bind data see this example using checkbox and also item.data,v should be a boolean

enter code here
<td><input type="checkbox" [(ngModel)]="item.done" /></td>
<td [ngSwitch]="item.done">
<span *ngSwitchCase="true">
      Yes
</span>
<span *ngSwitchDefault>
      No
</span>
</td>

and also u should use a filter to return data based on true or false

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