Angular2:RadioButton 更改事件未绑定

Angular2: RadioButton change event is not binding

在这里,我想简单地将RadioButton 与change 事件绑定。没有使用任何库。

以下工作正常。

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

但这一个不是 :

<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>

在将更改事件绑定到标签时,它给了我旧值。

谁能推荐正确的方法?

使用 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>

在 .ts 文件中,

  • 已添加import { ButtonRadioDirective } from 'ng2-bootstrap/components/buttons';

  • @Component注释中,将其作为directives: [ButtonRadioDirective]传递。

它工作正常。希望对你有用。

使用 Angular 2 RC2 不再需要创建 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>

并且:

class MyComp {
   food = 'fish';
}

来源:5thingsangular - Issue #8

没有库的双向绑定:

<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>