angular - 如何简化禁用/启用控制角度
问题描述
我有很多功能,例如:
changeScondPlace(event) {
if(event.checked == false) {
this.GeofencingSection.controls['location2longer'].disable();
this.GeofencingSection.controls['location2'].disable();
this.GeofencingSection.controls['location2days'].disable();
this.GeofencingSection.controls['picker2locationstart'].disable();
this.GeofencingSection.controls['picker2locationend'].disable();
this.GeofencingSection.controls['location2placement'].disable();
} else {
this.GeofencingSection.controls['location2longer'].enable();
this.GeofencingSection.controls['location2'].enable();
this.GeofencingSection.controls['location2days'].enable();
this.GeofencingSection.controls['picker2locationstart'].enable();
this.GeofencingSection.controls['picker2locationend'].enable();
this.GeofencingSection.controls['location2placement'].enable();
}
}
此功能将通过单击复选框将输入状态从禁用更改为启用或从启用更改为禁用。
我试过了
this.GeofencingSection.controls['location2longer'].enable = event.checked;
this.GeofencingSection.controls['location2'].enable = event.checked;
this.GeofencingSection.controls['location2days'].enable = event.checked;
this.GeofencingSection.controls['picker2locationstart'].enable = event.checked;
this.GeofencingSection.controls['picker2locationend'].enable = event.checked;
this.GeofencingSection.controls['location2placement'].enable = event.checked;
伙计们有没有办法简化该代码?
解决方案
changeScondPlace(event) {
if(event.checked == false) {
this.GeofencingSection.controls['location2longer'].disable();
this.GeofencingSection.controls['location2'].disable();
this.GeofencingSection.controls['location2days'].disable();
this.GeofencingSection.controls['picker2locationstart'].disable();
this.GeofencingSection.controls['picker2locationend'].disable();
this.GeofencingSection.controls['location2placement'].disable();
} else {
this.GeofencingSection.controls['location2longer'].enable();
this.GeofencingSection.controls['location2'].enable();
this.GeofencingSection.controls['location2days'].enable();
this.GeofencingSection.controls['picker2locationstart'].enable();
this.GeofencingSection.controls['picker2locationend'].enable();
this.GeofencingSection.controls['location2placement'].enable();
}
}
如果这些是您表单中的所有字段,您可以简单地尝试:
this.GeofencingSection.disable();
和 this.GeofencingSection.enable();
如果这些元素不是表单中的唯一元素,并且还有其他元素,那么也许您可以将这些控件放入this.GeofencingSection
对象中的一个组中。在这种情况下,如果您的组名是“someGroup”,您需要执行以下操作:
this.GeofencingSection.controls['someGroup'].disable();
和 this.GeofencingSection.controls['someGroup'].enable(); 该组下的所有表单控件将被禁用或启用。
推荐阅读
- java - 春季启动中@ControllerAdvice的junit测试
- r - 如何根据其他元素并行修改向量的元素?
- python - 如何让我的程序重置并添加将自动重新启动的继续提示重新启动程序
- php - PHP 测试脚本有效,其他脚本中的相同代码无效
- java - IBM MQ SSL 连接问题
- python - 使用 SVM 的 coef_ 函数的特征重要性
- python - Kivy 屏幕管理器无法在屏幕之间切换
- firebase - 执行 Firebase 规则时遇到问题
- javascript - Javascript 和 Django Rest Framework - 无法读取属性
- spring-boot - JdbcBatchItemWriter - setSql - 如何修复时间戳类型不匹配