【发布时间】:2021-01-23 21:37:03
【问题描述】:
app.component.html
<form #x="ngForm" (ngSubmit)="submit()">
<div class="form-check form-check-inline">
<input [(ngModel)]="details.a" #a="ngModel" type="checkbox" value="1">
<label class="form-check-label" for="inlineCheckbox1">HELLO A</label>
</div>
<div class="form-check form-check-inline">
<input [(ngModel)]="details.b" #b="ngModel" type="checkbox" value="2">
<label class="form-check-label" for="inlineCheckbox1">HELLO B</label>
</div>
<div class="form-check form-check-inline">
<input [(ngModel)]="details.c" #c="ngModel" type="checkbox" value="3">
<label class="form-check-label" for="inlineCheckbox1">HELLO C</label>
</div>
<button [disabled]="!x.valid" >Submit</button>
</form>
App.Component.ts
export class AppComponent implements OnInit {
details:{
a:boolean,
b:boolean,
c:boolean
} = {
a:null,
b:null,
c:null
}
constructor(){
}
submit(){
console.log(this.details) //to get true or false if checked
}
我正在尝试获取复选框的状态和值,如果选中,我需要将值分配给变量
示例:
如果选中 1 和 3 变量 x = 1 + 3 x = 4
【问题讨论】:
标签: angular typescript forms checkbox angular-reactive-forms