我正在尝试读取单选按钮值 - Angular 为 2,
索引.html
<input type="radio" id="options1" name="function" value="create">
<input type="radio" id="options2" name="function" value="continue">
索引.ts
@Component({
selector: 'function',
templateUrl: './client/components/function/index.html',
styleUrls: ['./client/components/function/index.css'],
providers: [],
directives: [ROUTER_DIRECTIVES]
})
我需要根据单选按钮值是创建还是继续在 html 中显示一个 div。
我试过的:
document.getElementById
获取 typescript 文件中单选按钮的值 - 未检测到属性 checked
。 model.function
来使用model
读取值。显然无法访问 model
变量! [(ngModel)]
- 这也不起作用。 请为此提出周转建议。
最佳答案
模板:
<input type="radio" id="options1" [(ngModel)]="myRadio" value="create">
<input type="radio" id="options2" [(ngModel)]="myRadio" value="continue">
然后在你的组件中定义一个
myRadio
变量并像这样初始化它:myRadio: string = ''
此变量将获得所选值。
并且, 不要 使用 javascript 来控制 Angular2 中的 DOM 元素,这违反了 angular2 哲学
关于javascript - 读取单选按钮值 - Angular 2,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39579309/