读取单选按钮值-Angular 2 [英] Reading radio button value - Angular 2
本文介绍了读取单选按钮值-Angular 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试读取单选按钮的值-角度2,
I'm trying to read the radio button value - in angular 2,
index.html
index.html
<input type="radio" id="options1" name="function" value="create">
<input type="radio" id="options2" name="function" value="continue">
index.ts
@Component({
selector: 'function',
templateUrl: './client/components/function/index.html',
styleUrls: ['./client/components/function/index.css'],
providers: [],
directives: [ROUTER_DIRECTIVES]
})
我需要根据是否创建或继续单选按钮值在html中显示div.
I need to display a div in the html based on if radio button value is create or continue.
我尝试过的事情:
- 使用
document.getElementById
获取打字稿文件中单选按钮的值-未检测到属性checked
. - 通过在打字稿中定义
model
来使用model.function
读取值.显然,无法访问model
变量! - 尝试使用
[(ngModel)]
-也不起作用.
- Fetching values of radio button in the typescript file using
document.getElementById
- Propertychecked
wasn't getting detected. - Reading the value using
model.function
by defining amodel
in the typescript. Unable to access themodel
variable, obviously! - Tried using
[(ngModel)]
- that didn't work either.
请为此提出建议.
推荐答案
模板:
<input type="radio" id="options1" [(ngModel)]="myRadio" value="create">
<input type="radio" id="options2" [(ngModel)]="myRadio" value="continue">
然后在您的组件中定义一个myRadio
变量并按如下所示对其进行初始化:
myRadio: string = ''
then define a myRadio
variable in your component and initialize it like this:
myRadio: string = ''
此变量将获得选定的值.
this variable will get the chosen value.
并且请勿使用javascript来控制Angular2中的DOM元素,这违反了angular2哲学
and, do not use javascript to control DOM elements in Angular2, this is against the angular2 philosophy
这篇关于读取单选按钮值-Angular 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文