admin 管理员组文章数量: 1184232
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
changeevent is fired only when the radio is checked. In your case you hardcoded the value you pass to your functiononPropertyChange('app'). You might want to change that toonPropertyChange(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
3 Answers
Reset to default 2With 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
@Componentannotation, passed it asdirectives: [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
版权声明:本文标题:javascript - Angular2: RadioButton change event is not binding - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744019495a2519545.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论