admin管理员组文章数量:1332359
I'm trying to build a Angular 2 ponent which displays a list of options with radios. It works fine but it the answer
field of the ponent, which is bound inside [(ng-model)]="answer"
, won't update when selecting one of the options. Am I doing something wrong or isn't this the way to create a list of radio selection options?
<div>
Answer: {{ answer }}
</div>
<div class="radio" *ng-for="#option of itemData">
<label>
<input type="radio" [value]="option.id" [(ng-model)]="answer"
(change)="responseChanged()" name="radio-list">
<span>{{ option.name }}</span>
</label>
</div>
Plunker
I'm trying to build a Angular 2 ponent which displays a list of options with radios. It works fine but it the answer
field of the ponent, which is bound inside [(ng-model)]="answer"
, won't update when selecting one of the options. Am I doing something wrong or isn't this the way to create a list of radio selection options?
<div>
Answer: {{ answer }}
</div>
<div class="radio" *ng-for="#option of itemData">
<label>
<input type="radio" [value]="option.id" [(ng-model)]="answer"
(change)="responseChanged()" name="radio-list">
<span>{{ option.name }}</span>
</label>
</div>
Plunker
Share Improve this question edited Mar 31, 2021 at 8:09 Syscall 19.8k10 gold badges43 silver badges58 bronze badges asked Oct 27, 2015 at 13:00 lucassplucassp 4,1673 gold badges28 silver badges36 bronze badges 1- 1 Here's a custom implementation for radio, don't know about lists though. – Eric Martinez Commented Oct 27, 2015 at 20:50
2 Answers
Reset to default 6Well i guess two way binding is now working with radio, so currently you cannot use [(ng-model)]
.
The alternative is to use the change event and checked attribute. See my plunker
https://plnkr.co/edit/7Zm3qgoSv22Y9KrBn4tS?p=preview
(change)="answer=$event.target.value"
and
[checked]='answer==option.id'
You cannot use ng-model with radio boxes like in angular1. However there are several ponents on github that allows you to do it easily, like ng2-radio-group ponent. It has support for both radio select and multiple checkboxes select:
<radio-group [(ngModel)]="sortBy">
<input type="radio" value="rating"> Rating<br/>
<input type="radio" value="date"> Date<br/>
<input type="radio" value="watches"> Watch count<br/>
<input type="radio" value="ments"> Comment count<br/>
</radio-group>
<checkbox-group [(ngModel)]="orderBy">
<input type="checkbox" value="rating"> Rating<br/>
<input type="checkbox" value="date"> Date<br/>
<input type="checkbox" value="watches"> Watch count<br/>
<input type="checkbox" value="ments"> Comment count<br/>
</checkbox-group>
本文标签: javascriptModel won39t update in Angular 2 radio listStack Overflow
版权声明:本文标题:javascript - Model won't update in Angular 2 radio list - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742315798a2451789.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论