【问题标题】:Angular 2 how to bind check property of radiobutton to boolean attribute of component?Angular 2如何将单选按钮的检查属性绑定到组件的布尔属性?
【发布时间】:2016-09-30 10:16:43
【问题描述】:

如何将单选按钮的选中属性绑定到组件类的布尔变量?我想要的是,如果设置了布尔标志,则默认情况下会检查模板中的单选按钮。

我试过<input type="radio" [(ngModel)]="isSelected()">,但它会抛出模板语法错误

platform-browser.umd.js:962EXCEPTION: Error: Uncaught (in promise): Template parse errors:
Parser Error: Unexpected token '=' at column 13 in [isSelected()=$event] in ChoiceComponent@5:6 ("
                    (change)="select()"
      required
      [ERROR ->][(ngModel)]="isSelected()"> {{choice.text}}</label>
    </div>
    "): ChoiceComponent@5:6

【问题讨论】:

  • [(ngModel)]="isSelected()" 无效。您不能双向绑定到函数。你的代码的意图是什么? isSelected() 长什么样子?

标签: javascript html css typescript angular


【解决方案1】:

收音机还没有得到很好的支持。另见Angular 2 forms; ngFormControl for radio and select

在解决此问题之前,您可以尝试类似的方法

<input type="radio" [ngModel]="{selected: model.sex == 'male'}" (ngModelChange)="model.sex='male'"  name="sex" value="male">Male<br>

另见How to bind to radio buttons in angular2 beta 6

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 1970-01-01
    • 2012-01-21
    • 2012-03-02
    相关资源
    最近更新 更多