作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下代码
if (this.myForm.value['isDefault'] === true)
其中 isDefault 是复选框 FormControl。现在,如果选中该复选框,我期望 this.myForm.value['isDefault'] 结果为 true。当我发出警报时,它确实显示为 true,但这种比较不会导致 true。
最佳答案
您需要重新检查 TS 文件中的表单和代码。我重现它仍然可以正常工作。
HTML
<div class="container">
<div>
<form [formGroup]="theForm" (submit)="check()">
Check? <input type='checkbox' formControlName="firstCheck"/>
<input type="submit" value="check">
</form>
</div>
</div>
TS
export class AppComponent {
theForm: FormGroup;
constructor(private fb: FormBuilder) {
this.theForm = fb.group({
firstCheck: false
})
}
check() {
console.log(this.theForm.value['firstCheck'] === true)
}
}
演示 https://stackblitz.com/edit/checkbox-reactive-aagwtp?file=app%2Fapp.component.ts
关于javascript - 读取 Angular Reactive Form 中的复选框 FormControl 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56794530/
我是一名优秀的程序员,十分优秀!