javascript - IF Statement in TypescriptAngular - Stack Overflow

I am trying to get a the correct pop-up message depending on a users input. If the user is a 'Con

I am trying to get a the correct pop-up message depending on a users input.

If the user is a 'Con Owner' they must fill out all items labelled as 'Con'. If they do not, they should have a warning message when they click submit.

I have written the below to loop through each item and where the owner of that item is equal to Con and the Value is empty it should give me the warning message. If they have filled it in, it should be the confirmation message.

However, the below isn't giving me the results I require. Irrespective of what happens, I always get the warning message as a pop up, even if the value has been populated.

  isValuePopulated() {  
    let attrData = this.attributes.attrData;
    let isAttributeCompleted = true;
    attrData.forEach(item=>{
      if(item.owner==='Con' && monFunctions.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);
    }
  }

Edit: Thanks for the ments so far I have since removed the monFunctions.isEmptyString(item.value)) as suggest. But I assume the error is down to the loop being asyc (also mentioned). However, as I am new to this, I am not sure how to resolve this. My first thought was to have two methods and have one loop through the items and then push the value to another method which determines the pop up. But I keep getting a "Expression Expected" error message when trying to build.

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

I am trying to get a the correct pop-up message depending on a users input.

If the user is a 'Con Owner' they must fill out all items labelled as 'Con'. If they do not, they should have a warning message when they click submit.

I have written the below to loop through each item and where the owner of that item is equal to Con and the Value is empty it should give me the warning message. If they have filled it in, it should be the confirmation message.

However, the below isn't giving me the results I require. Irrespective of what happens, I always get the warning message as a pop up, even if the value has been populated.

  isValuePopulated() {  
    let attrData = this.attributes.attrData;
    let isAttributeCompleted = true;
    attrData.forEach(item=>{
      if(item.owner==='Con' && monFunctions.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);
    }
  }

Edit: Thanks for the ments so far I have since removed the monFunctions.isEmptyString(item.value)) as suggest. But I assume the error is down to the loop being asyc (also mentioned). However, as I am new to this, I am not sure how to resolve this. My first thought was to have two methods and have one loop through the items and then push the value to another method which determines the pop up. But I keep getting a "Expression Expected" error message when trying to build.

  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);
  }
}
Share Improve this question edited Nov 23, 2019 at 17:22 LearnerCode asked Nov 23, 2019 at 16:27 LearnerCodeLearnerCode 1991 gold badge7 silver badges18 bronze badges 6
  • meed to see some sample data of what attrData might look like.. also monFunctions.isEmptyString() is very not needed... just do !item.value – bryan60 Commented Nov 23, 2019 at 16:32
  • You should just use a debugger to see what is happening. – hansmaad Commented Nov 23, 2019 at 16:33
  • Check if it enters the if statement. May be your if statement is always false – Angela Amarapala Commented Nov 23, 2019 at 16:34
  • forEach is async your if condition is being executed before loop is finished. – Talha Junaid Commented Nov 23, 2019 at 16:47
  • @TalhaJunaid pletely false – bryan60 Commented Nov 23, 2019 at 17:16
 |  Show 1 more ment

3 Answers 3

Reset to default 2

While the point thats made by @Jérôme is true, the issue you face is because you expect an async code block to run syncroniously. You can use a simple for-of loop instead.

for (const item of arrayData)

As a ment said, replace your function isEmptyString() by !item.value. The condition if (item.value) will evaluates to true if item.value is NOT :

  • undefined
  • empty string
  • NaN
  • null
  • 0
  • false

Source

Thanks for the suggestions. I didn't realise the loop I was orignally using was async and didn't realise there is alternative non-async loop solution The following code worked:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745347976a4623652.html

相关推荐

  • javascript - IF Statement in TypescriptAngular - Stack Overflow

    I am trying to get a the correct pop-up message depending on a users input. If the user is a 'Con

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信