gpt4 book ai didi

javascript - Typescript/Angular 中的 IF 语句

转载 作者:行者123 更新时间:2023-11-30 10:58:33 24 4
gpt4 key购买 nike

我正在尝试根据用户输入获取正确的弹出消息。

如果用户是“Con Owner”,他们必须填写所有标记为“Con”的项目。如果他们不这样做,他们应该在单击提交时收到一条警告消息。

我已经编写了下面的代码来遍历每个项目,并且该项目的所有者等于 Con 并且值为空,它应该给我警告消息。如果他们填写了,应该是确认信息。

但是,以下内容并未提供我需要的结果。无论发生什么情况,我总是会收到弹出的警告消息,即使值已被填充也是如此。

  isValuePopulated() {  
let attrData = this.attributes.attrData;
let isAttributeCompleted = true;
attrData.forEach(item=>{
if(item.owner==='Con' && commonFunctions.isEmptyString(item.value)){
isAttributeCompleted = false;
}}
);
if (isAttributeCompleted){
this.showErrorInfoPopUp('All correct!!!!', 'Confirmation', true);
} else {
this.setErrorPopUpButtonStatus(true, false, true);
this.showErrorInfoPopUp('Are you sure you want to continue?', 'Warning', true);
}
}

编辑:感谢到目前为止的评论,我已经按照建议删除了 commonFunctions.isEmptyString(item.value))。但我认为错误是由于循环是异步的(也提到过)。但是,由于我是新手,我不确定如何解决这个问题。我的第一个想法是有两个方法,一个循环遍历项目,然后将值推送到另一个确定弹出窗口的方法。但是我在尝试构建时不断收到“预期表达式”错误消息。

  isValuePopulated() {  
let attrData = this.attributes.attrData;
let isAttributeCompleted = true;
attrData.forEach(item=>{
if(item.owner==='Con' && !item.attributeValue){
isAttributeCompleted = false;
}}
);
this.typeOfPopUp(isAttributeCompleted )
}

typeOfPopUp(isAttributeCompleted ){

if (isAttributeCompleted ){
this.setErrorPopUpButtonStatus(true, false, true);
this.showErrorInfoPopUp('All correct!!!!', 'Confirmation', true);
} else {
this.setErrorPopUpButtonStatus(true, false, true);
this.showErrorInfoPopUp('Are you sure you want to continue?, 'Warning', true);
}
}

最佳答案

虽然@Jérôme 的观点是正确的,但您面临的问题是因为您希望异步代码块同步运行。您可以改用简单的 for-of 循​​环。

for (const item of arrayData)

关于javascript - Typescript/Angular 中的 IF 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59009737/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com