Using * ngFor to create a series of switches for angular2 using the materialize-css framework

Seasons greet everyone!

I have the following code that builds one radio button based on the materialize-css framework http://materializecss.com/forms.html#radio

<input name = 'group1'
       type = 'radio'
       id = 'test2'/>
<label for = 'test2'>Yellow</label>

My attempt to use * ngFor is shown below:

  statuses: string[] = [
    'Single',
    'Married',
    'Divorced',
    'Common-law',
    'Visiting'
  ];

  <p>{{maritalStatus?.status}}</p>
  <div *ngFor = 'let status of statuses;  let indx = index'>
    <input #widget
           class = 'with-gap'
           name = 'statusGroup'
           type = 'radio'
           id = 'status'
           [value] = 'status'
           [(ngModel)] = 'maritalStatus.status'
           (change) = 'radioBtnChange$.next(status)'
    />
    <label for = 'status'>{{status}}</label>
    <p>{{status}}{{ indx}}</p>
  </div>

All buttons are created, but only the first button (single) can be selected.

How can I make a series of buttons function like radio buttons that are expected to do?

thank

+4
source share
1 answer

Plunker

Why does it not work

status *ngFor for label id input.

:

, :

<input [id]="status">

() value.

. Angular ; HTML-, .

Interpolation

, , :

<input id="{{status}}">

, Angular , .

?

.

HTML

<h2>Current Status</h2>
<p>{{maritalStatus?.status}}</p>

<h2>Options</h2>
<div *ngFor="let status of statuses; let indx = index">
  <input #widget
   class='with-gap'
   name='statusGroup'
   type='radio'
   [id]='status'
   [value]='status'
   [(ngModel)]='maritalStatus.status'
  />
  <label [for]='status'>{{status}}</label>
</div>

import {Component} from '@angular/core';
import {Http} from '@angular/http'
import {bootstrap} from '@angular/platform-browser-dynamic';

@Component({
  selector: 'material-app',
  templateUrl: 'app.component.html'
})
export class AppComponent {
  maritalStatus = { status: 'Nothing selected' };
  statuses: string[] = [
    'Single',
    'Married',
    'Divorced',
    'Common-law',
    'Visiting'
  ];
  constructor() { }

}

- Angular 2 < 2.2.0

Angular 2, 2.2.0, label for :

<label [attr.for]='status'>{{status}}</label>

for label.

?

Angular 2.2.0 (634b3bb), Angular for htmlFor.

, , .

, Pascal Precht .

+5

Source: https://habr.com/ru/post/1664808/


All Articles