我有以下 Typescript 布尔函数:
checkRiskValues(): boolean {
this.controlsName.forEach((item) => {
this.threatForm.get(item)!.valueChanges.subscribe(value => {
this.valueControlArray.push(this.threatForm.get(item)!.value);
if (this.valueControlArray.indexOf(true) > -1)
return true;
else
return false
});
});
}
Run Code Online (Sandbox Code Playgroud)
该方法出现错误,函数必须返回值。我知道,但我不确定如何在 foreach 范围之外实现这个 true/false 语句?
如果我在 foreach 循环之外调用 if/else 语句,结果总是 false,因为
if (this.valueControlArray.indexOf(true) > -1)
Run Code Online (Sandbox Code Playgroud)
是空的..
编辑
我删除了 checkRiskValues() 函数并在 ngOnInit() 方法中添加了完整的逻辑,并且还添加了 valueControl 变量来保留 true/false 值并传入另一个函数。谢谢大家..这是我的解决方案:
ngOnInit() {
this.controlsName.forEach((item) => {
this.threatForm.get(item)!.valueChanges.subscribe(value => {
this.valueControlArray.push(this.threatForm.get(item)!.value);
if (this.valueControlArray.indexOf(true) > -1) {
this.valueControl = true;
}
else {
this.valueControl = false;
}
});
});
}
Run Code Online (Sandbox Code Playgroud)
这是我的建议:
checkRiskValues(): boolean {
return setRiskValues().indexOf(true) > -1;
}
setRiskValues(): Array<any> {
let values = [];
this.controlsName.forEach((item) => {
this.threatForm.get(item)!.valueChanges.subscribe(value => {
values.push(this.threatForm.get(item)!.value);
});
});
return values;
}
Run Code Online (Sandbox Code Playgroud)
这样你就:
| 归档时间: |
|
| 查看次数: |
47857 次 |
| 最近记录: |