模型不会在 Angular 2 电台列表中更新
Model won't update in Angular 2 radio list
我正在尝试构建一个 Angular 2 组件,它显示带有收音机的选项列表。它工作正常,但组件的 answer
字段(绑定在 [(ng-model)]="answer"
内)在选择其中一个选项时不会更新。我是不是做错了什么,或者这不是创建单选选项列表的方法吗?
<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>
好吧,我想双向绑定现在可以与无线电一起使用,所以目前您不能使用 [(ng-model)]
。
替代方法是使用更改事件和检查属性。看我的笨蛋
https://plnkr.co/edit/7Zm3qgoSv22Y9KrBn4tS?p=preview
(change)="answer=$event.target.value"
和
[checked]='answer==option.id'
您不能像在 angular1 中那样将 ng-model 与单选框一起使用。但是 github 上有几个组件可以让您轻松地做到这一点,例如 ng2-radio-group 组件。它支持单选 select 和多个复选框 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="comments"> 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="comments"> Comment count<br/>
</checkbox-group>
我正在尝试构建一个 Angular 2 组件,它显示带有收音机的选项列表。它工作正常,但组件的 answer
字段(绑定在 [(ng-model)]="answer"
内)在选择其中一个选项时不会更新。我是不是做错了什么,或者这不是创建单选选项列表的方法吗?
<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>
好吧,我想双向绑定现在可以与无线电一起使用,所以目前您不能使用 [(ng-model)]
。
替代方法是使用更改事件和检查属性。看我的笨蛋
https://plnkr.co/edit/7Zm3qgoSv22Y9KrBn4tS?p=preview
(change)="answer=$event.target.value"
和
[checked]='answer==option.id'
您不能像在 angular1 中那样将 ng-model 与单选框一起使用。但是 github 上有几个组件可以让您轻松地做到这一点,例如 ng2-radio-group 组件。它支持单选 select 和多个复选框 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="comments"> 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="comments"> Comment count<br/>
</checkbox-group>