模型不会在Angular 2无线电列表中更新 [英] Model won't update in Angular 2 radio list

查看:63
本文介绍了模型不会在Angular 2无线电列表中更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个Angular 2组件,该组件显示带有无线电的选项列表。它工作正常,但它是组件的答案字段,它绑定在 [(ng-model)] =answer将不会更新。我做错了什么或者这不是创建无线电选择选项列表的方法吗?

I'm trying to build a Angular 2 component which displays a list of options with radios. It works fine but it the answer field of the component, 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

推荐答案

我猜双向绑定现在正在使用无线电,所以目前你不能使用 [(ng-model)]

Well i guess two way binding is now working with radio, so currently you cannot use [(ng-model)].

另一种方法是使用change事件和checked属性。查看我的plunker

The alternative is to use the change event and checked attribute. See my plunker

http://plnkr.co/edit/ 7Zm3qgoSv22Y9KrBn4tS?p =预览

(更改)=​​answer = $ event.target.value

[checked] ='answer == option.id'

这篇关于模型不会在Angular 2无线电列表中更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆