Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Typescript/Angular中的IF语句_Javascript_Angular_Typescript_If Statement - Fatal编程技术网

Javascript 在Typescript/Angular中的IF语句

Javascript 在Typescript/Angular中的IF语句,javascript,angular,typescript,if-statement,Javascript,Angular,Typescript,If Statement,我正试图根据用户的输入获取正确的弹出消息 如果用户是“Con所有者”,他们必须填写所有标记为“Con”的项目。如果没有,则在单击“提交”时应显示警告消息 我已经写了下面的内容来循环遍历每个项目,如果该项目的所有者等于Con,并且值为空,那么应该给我一条警告消息。如果他们已经填写了,应该是确认信息 然而,下面的内容并没有给出我需要的结果。无论发生什么情况,我总是以弹出窗口的形式收到警告消息,即使该值已填充 isValuePopulated() { let attrData = th

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

如果用户是“Con所有者”,他们必须填写所有标记为“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))
。但是我假设错误是由于循环是asyc(也提到了)。然而,由于我是新手,我不知道如何解决这个问题。我的第一个想法是有两个方法,一个循环遍历项目,然后将值推送到另一个方法,该方法决定弹出窗口。但我在尝试构建时不断收到“Expression Expected”错误消息

  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);
  }
}

正如一条评论所说,将函数isEmptyString()替换为!项目价值。 如果item.value不是,则条件if(item.value)将计算为true:

  • 未定义
  • 空字符串
  • 空的
  • 0
  • 假的

正如一条评论所说,将函数isEmptyString()替换为!项目价值。 如果item.value不是,则条件if(item.value)将计算为true:

  • 未定义
  • 空字符串
  • 空的
  • 0
  • 假的

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

for (const item of arrayData)

尽管@Jérôme所说的观点是正确的,但您面临的问题是,您希望异步代码块同步运行。 您可以使用一个简单的for of循环

for (const item of arrayData)

谢谢你的建议。我没有意识到我最初使用的循环是异步的,也没有意识到有替代的非异步循环解决方案 以下代码有效:

  isValuePopulated() {  
    let attrData = this.attributes.attrData;
    let attributeCompleted: boolean = true;
    for (const item of attrData){
      if(item.owner==='Con' && !item.value){
        attributeCompleted = false;
      }

    }
    if (attributeCompleted){
      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);
    }
  }

谢谢你的建议。我没有意识到我最初使用的循环是异步的,也没有意识到有替代的非异步循环解决方案 以下代码有效:

  isValuePopulated() {  
    let attrData = this.attributes.attrData;
    let attributeCompleted: boolean = true;
    for (const item of attrData){
      if(item.owner==='Con' && !item.value){
        attributeCompleted = false;
      }

    }
    if (attributeCompleted){
      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);
    }
  }

我想看看ATRTDATA可能是什么样子的一些示例数据。。另外,
commonFunctions.isEmptyString()
是非常不需要的。。。只要做
!item.value
您应该只使用调试器来查看发生了什么。检查它是否输入了if语句。可能是您的if语句始终为falseforEach is
async
您的if条件是在循环完成之前执行的。@TalhaJunaid完全为falsemeed,以查看attrData的某些示例数据。。另外,
commonFunctions.isEmptyString()
是非常不需要的。。。只要做
!item.value
您应该只使用调试器来查看发生了什么。检查它是否输入了if语句。可能是您的if语句始终为false foreach is
async
if条件在循环完成之前执行。@TalhaJunaid完全为false谢谢。我已将此评论和原始评论记录在案。并更新了帖子。请见谅,谢谢。我已将此评论和原始评论记录在案。并更新了帖子。请参见编辑