【发布时间】:2020-07-16 19:28:45
【问题描述】:
我有一个多输入表单,它们都是类型单选按钮。 我需要设置“必需”和“默认检查”属性,问题是 Angular 无法识别其中任何一个。 试图有一个默认的检查项目,我已经尝试过: 使用检查,检查=真,检查=“真”,[检查]=“真”,[检查]=“{1 ==1}”...
到目前为止,还没有任何工作。 “必填”也是一样。
也许我在 TypeScript 方面需要做些什么? 我正在使用 Angular 9,更具体的 @angular-devkit/core": "9.0.6"
这是我正在做的一个小例子:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-tables',
templateUrl: './perso.component.html',
styleUrls: ['./perso.component.css']
})
export class PersoComponent implements OnInit {
q1: number;
process() {
console.log(this.q1);
}
}
<form>
<div class="row">
<p>1. I am the life of the party:</p>
<label><input type="radio" name="q1" value="1" [(ngModel)]="q1" required>Very Inaccurate</label>
<br><label><input type="radio" name="q1" value="2" [(ngModel)]="q1">Inaccurate</label>
<br><label><input type="radio" name="q1" value="3" [(ngModel)]="q1" checked="true">Neither Accurate Nor Inaccurate</label>
<br><label><input type="radio" name="q1" value="4" [(ngModel)]="q1">Moderately Accurate</label>
<br><label><input type="radio" name="q1" value="5" [(ngModel)]="q1">Very Accurate</label>
{{'\nValue of q1: '+q1}}
<hr>
</div>
<button
type="submit"
class="btn btn-info btn-fill pull-right"
(click)="process()"
>Process</button>
<div class="clearfix"></div>
</form>
提前感谢您的帮助
【问题讨论】:
-
stackoverflow.com/questions/43780840/… [value] 你试过了吗?
-
@Kimchy 是的,我使用过 [value] = true 和 [value] = "true"
-
@Supercool。谢谢,我已经尝试了链接中的方法。在 StackBlitz 示例中: [checked]="radiobutton === model.option" ,"radiobutton === model.option" 的值是 true,所以我回到:[已检查]="true" 遗憾的是,它不起作用
标签: html angular radio-button