当有 ngModel 时,检查 angular 2 中的单选按钮 [英] check radio button in angular 2 when there is ngModel

查看:33
本文介绍了当有 ngModel 时,检查 angular 2 中的单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Angular 2 项目中有 ngModel 并且单选按钮没有检查它

In my Angular 2 project there is ngModel and the radio button doesn't check it

<input type="radio" id="user" name="user" value="RETAIL" [(ngModel)]="myRadio" check>

但是当我剪切 ngModel 时,它会检查

But when I cut the ngModel it check

<input type="radio" id="user" name="user" value="RETAIL check>

但是我不能剪切它,因为我需要ngModel,有没有其他方法可以用ngModel检查加载页面上的单选按钮?

But I can't cut it because I need ngModel, is there another way to check radio button on the load page with ngModel?

推荐答案

如果输入元素的值是字符串文字,请使用以下两种形式之一声明它:

If the value of the input element is a string literal, declare it using one of the two following forms:

<input type="radio" id="user" name="user" value="My value 1" [(ngModel)]="myRadio" />
<input type="radio" id="user" name="user" [value]="'My value 1'" [(ngModel)]="myRadio" />

如果值不是字符串文字,使用[value]:

If the value is not a string literal, use [value]:

<input type="radio" id="user" name="user" [value]="MyValue1" [(ngModel)]="myRadio" />


要默认选中单选按钮,您应该将 myRadio 初始化为适当的值:

对于字符串值:

public myRadio: string = "My value 1";

对于另一种类型的数据:

For another type of data:

public myRadio: MyDataType = MyValue1;


您可以在 this plunker 中看到正在运行的代码.


You see the code at work in this plunker.

这篇关于当有 ngModel 时,检查 angular 2 中的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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