最好的想法是利用Angular的做事方式。在这种情况下
Forms API
从…起
@angular/forms
。
你可以利用这个api来完成大部分繁重的工作。绑定
formControlName
到
FromGroup
对象,并使用您设置的关键点中的值。
import { FormBuilder, FormGroup } from '@angular/forms';
export class AppComponent {
form: FormGroup;
constructor(private formB: FormBuilder) {}
ngOnInit(): void {
this.form = this.formB.group({
checkBox1: [false],
checkBox2: [false],
});
}
submit() {
console.log(this.form.value);
}
}
样板
<form [formGroup]="form">
<div class="widget-container">
<input type="checkbox" formControlName="checkBox1" />Heading1
<input type="checkbox" formControlName="checkBox2" />Heading2
<div>
<button type="button" (click)="submit()">Submit</button>
</div>
</div>
</form>
这样你就习惯了Angular在这些普通情况下使用的工具。