Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2: how to create radio buttons from enum and add two-way binding?

I'm trying to use Angular2 syntax to create radio buttons from an enum definition, and bind the value to a property that has the type of that enum.

My html contains:

<div class="from_elem">
    <label>Motif</label><br>
    <div  *ngFor="let choice of motifChoices">
        <input type="radio" name="motif" [(ngModel)]="choice.value"/>{{choice.motif}}<br>
    </div>
</div>

In my @Component, I declared the set of choices and values:

private motifChoices: any[] = [];

And in the constructor of my @Component, I filled the choices the following way:

constructor( private interService: InterventionService )
{
    this.motifChoices =
        Object.keys(MotifIntervention).filter( key => isNaN( Number( key )))
            .map( key => { return { motif: key, value: false } });
}

The radio buttons are displayed correctly, now I seek to bind the value selected to a property. But when I click one of the buttons the value choice.value is set to undefined.

like image 622
Anthony Brenelière Avatar asked Sep 08 '16 13:09

Anthony Brenelière


People also ask

How to use radio buttons with angular reactive forms?

Radio Buttons with Angular Reactive Forms Reactive Forms Radio Button Validation Set Radio Button Selected in Angular Working with Radio Buttons in Template-driven Form in Angular Before working with radio buttons in template-driven form, we need to activate FormsModuleservice in angular app.

How to Data Bind radio buttons in angular using unidirectional data flow?

Learn how to data bind radio buttons in Angular using unidirectional data flow. Assume you have a simple list or table of elements, each row having a radio button. The user can select one of the rows and your task is to determine the selected row entry. We basically need to establish some data binding between our data and the radio buttons.

How to achieve two-way binding in radiobutton using ngmodel?

The steps to achieve two-way binding in RadioButton are as follows, Initialize RadioButton component and bind the checked value using ngModel as in the below code using “banana in a box” syntax,

Which components are generally used using Angular Material?

Components such as autocomplete, date picker, slider, menus, grids, toolbars, and radio buttons are generally used using Angular Material. A radio button is a simple input element with a type property set to radio.


1 Answers

Ok finally I found out the solution. I am currenly using Angular 2 RC5.

The enum value I want to bind my radio is the property:

intervention.rapport.motifIntervention : MotifInterventions

In my @Component I declared private members to give access to enum definition in the html template:

export class InterventionDetails
{
    private MotifIntervention = MotifIntervention;
    private MotifInterventionValues = Object.values(MotifIntervention).filter( e => typeof( e ) == "number" );

    // model object:
    private intervention: Intervention;

Here is HTML code for the radio buttons:

<div *ngFor="let choice of MotifInterventionValues">
    <input type="radio"
           [(ngModel)]="intervention.rapport.motifIntervention"
           [checked]="intervention.rapport.motifIntervention==choice"
           [value]="choice" />
    {{MotifIntervention[choice]}}<br>
</div>
  • [(ngModel)]="intervention.rapport.motifIntervention" is two-way binding, it is required to update the property in the model (in my case intervention.rapport.motifIntervention)

  • [checked]="intervention.rapport.motifIntervention==choice" is required to update the radio buttons component if the value intervention.rapport.motifIntervention is modified externally.

  • [value]="choice" is the value that is assigned to my property when the radio button is selected.

  • {{MotifIntervention[choice]}} is the label of the radio button

like image 184
Anthony Brenelière Avatar answered Sep 18 '22 08:09

Anthony Brenelière