javascript - 如何以反应形式存储布尔值?
问题描述
我想以反应形式存储布尔值,其中按钮的值为真或假。这样我就可以访问这些按钮的输入。问题还在于我在此页面中有另一个表单,用于文本输入。所以我真的不知道我是否可以使用该表格两次。
<ion-form [formGroup]="form" (ngSubmit)="takeBools()">
<ion-grid>
<ion-row>
<ion-col>
<ion-button class="button1" (click)="onLoc()" shape="round" [color]="local ? 'light' : 'grey'"> <ion-icon name="pin"></ion-icon> </ion-button>
<ion-col>
<ion-row>
<ion-grid>
</ion-form>
<form [formGroup]="form" (ngSubmit)="addTag()">
<ion-item>
<ion-input formControlName="tag" clearInput="true" placeholder="Tags" name="tagValue"></ion-input>
<ion-button item-right type="submit" icon-only>
<ion-icon name="checkmark"></ion-icon>
</ion-button>
</ion-item>
</form>
页面.ts
form: FormGroup;
public local: boolean = true;
...
addTag() { // properly access and reset reactive form values
const tagCtrl = this.form.get('tag');
if (tagCtrl.value) {
this.tagList.push(tagCtrl.value);
this.tagInput = ''; // in order to have an empty input
}
}
takeBools() {
const local: boolean = this.form.controls['local'].value;
console.log(this.local);
}
ngOnInit() {
this.form = new FormGroup({
tag: new FormControl(null, {
updateOn: 'submit',
validators: [Validators.required, Validators.maxLength(20), Validators.minLength(1)]
}),
local: new FormControl(true, { // these bools are not accesable yet
updateOn: 'submit',
}),
})
解决方案
我认为您要做的是local
从FormGroup
.
您可以使用以下代码执行此操作。
const local: boolean = this.form.controls[‘local’].value;
推荐阅读
- amazon-web-services - 在资源创建中使用集合映射
- c - 调用者线程上的`pthread_cancel`?
- gooddata - 如何将数据分析迁移到 AWS?
- sql - 获取具有重叠繁忙时间的可用日期间隔
- kubernetes - 如何将来自 kind k8s 集群的传出流量转发到 sshuttle 连接的网络?
- angular - 每当我尝试从控制台构建一个新的离子项目时,它都会给我,npm ERR!无法读取未定义的属性(读取“isServer”)
- webhooks - 如何使用 API 触发的 webhook 回复自定义标头
- typescript - 在 TypeScript 中从泛型类型初始化属性
- db2 - 无法为 IBM Cloud Db2 选择 Lite 套餐
- api - REST API 中的什么响应代码对应于由于查询参数而找不到资源?