[英]Reading radio button value - Angular 2
我正在嘗試讀取單選按鈕值 - 以角度2,
的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。
我嘗試過的:
document.getElementById
獲取typescript文件中單選按鈕的值 - 未檢測到屬性checked
。 model.function
定義model
,使用model.function
讀取值。 顯然,無法訪問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哲學的
如果您只有2或3個無線電選項,最簡單的選擇是使用模板引用變量,如下所示:
<input #op1 type="radio" id="options1" name="function" value="create">
<input #op2 type="radio" id="options2" name="function" value="continue">
<button (click)="fn(op1.checked ? op1.value : op2.checked ? op2.value)">Run</button>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.