admin管理员组

文章数量:1355703

Here, I want to simply bind RadioButton with change event. Not using any library.

Following works fine.

<input type="radio" name="test" value="A" (change)="onPropertyChange('test')">
<input type="radio" name="test" value="B" (change)="onPropertyChange('test')">

But this one is not :

<div class="btn-group col-lg-2" data-toggle="buttons" >
<label *ngFor=" let app of applications; let i = index; " 
       class="btn btn-default " [ngClass]="{'active':( ticket.app == app)}">      
      <input type="radio" id="app{{i}}"  name="app" value="{{i}}"                   
           checked="{{ ( ticket.app == app ) ? 'checked' : ''}}" (change)=" 
           onPropertyChange('app')"  > 
     {{app}}
</label>
</div>

While binding change event to label, it is giving me old value.

Can anyone suggest right approach?

Here, I want to simply bind RadioButton with change event. Not using any library.

Following works fine.

<input type="radio" name="test" value="A" (change)="onPropertyChange('test')">
<input type="radio" name="test" value="B" (change)="onPropertyChange('test')">

But this one is not :

<div class="btn-group col-lg-2" data-toggle="buttons" >
<label *ngFor=" let app of applications; let i = index; " 
       class="btn btn-default " [ngClass]="{'active':( ticket.app == app)}">      
      <input type="radio" id="app{{i}}"  name="app" value="{{i}}"                   
           checked="{{ ( ticket.app == app ) ? 'checked' : ''}}" (change)=" 
           onPropertyChange('app')"  > 
     {{app}}
</label>
</div>

While binding change event to label, it is giving me old value.

Can anyone suggest right approach?

Share Improve this question edited Oct 16, 2019 at 17:03 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jun 20, 2016 at 2:51 JacksJacks 511 gold badge2 silver badges6 bronze badges 6
  • 1 You should not rely on Bootstrap javascript when you deal with Angular. Your problem is data-toggle="buttons" – yurzui Commented Jun 20, 2016 at 4:58
  • add onPropertyChange Function ts code – mayur Commented Jun 20, 2016 at 5:01
  • 1 plnkr.co/edit/YMQcwsBW0ems1dyEGJ8G?p=preview – yurzui Commented Jun 20, 2016 at 5:30
  • The change event is fired only when the radio is checked. In your case you hardcoded the value you pass to your function onPropertyChange('app'). You might want to change that to onPropertyChange(i) – null canvas Commented Jun 20, 2016 at 7:18
  • Thanks @yurzui for suggesting right approach. Very helpful. – Jacks Commented Jun 20, 2016 at 18:32
 |  Show 1 more ment

3 Answers 3

Reset to default 2

With Angular 2 RC2 it’s no longer needed to create the RadioButtonState:

Radio Buttons can now share FormControl instance

<form #f="ngForm">
   <input type="radio" name="food" [(ngModel)]="food" value="chicken">
   <input type="radio" name="food" [(ngModel)]="food" value="fish">
</form>

And:

class MyComp {
   food = 'fish';
}

Source: 5thingsangular - Issue #8

Using ng2-bootstrap,

<div class="btn-group col-lg-2">
    <label *ngFor="let app of applications" class="btn btn-default" [(ngModel)]="ticket.app" btnRadio="{{app}}">{{app}}</label>
</div>

In .ts file,

  • Added import { ButtonRadioDirective } from 'ng2-bootstrap/ponents/buttons';

  • In @Component annotation, passed it as directives: [ButtonRadioDirective].

It works fine. Hope it will work for you.

Two way binding without a library:

<div class="btn-group" data-toggle="buttons">
    <label class="btn btn-default" [class.active]="yourVariable==item" (click)="yourVariable=item" *ngFor="let item of items">
        <input type="radio" style="display: none;" name="radios" [(ngModel)]="yourVariable" [value]="item" [checked]="yourVariable==item" />
        {{yourLabelText}}
    </label>
</div>

本文标签: javascriptAngular2 RadioButton change event is not bindingStack Overflow