0

In my angular project, I have some booleans that trigger my NgIfs. I want to be able to reset all booleans to false and then set the one relevant boolean to true.

here is an example of my code:

resetBooleans(){
   this.mainBoolean = false;
   this.secondBoolean = false;
   this.thirdBoolean = false;
}

doSecondThing(){
   this.resetBooleans();
   this.secondBoolean = true;

   //do second thing logic
}

Whenever I run this, it resets all the booleans to false and does not then turn this.secondBoolean back to true.

I have tried everything in this post but none of it seems to be working for me.

PS: I'm not interested in using timeouts

Thanks!

update: I added a console.log(this.secondBoolean) and it came back true, but it is not triggering the NgIf in my HTML... Anyone know why not?

second update:

it isn't the HTML, because this works fine:

doSecondThing(){
   this.mainBoolean = false;
   this.thirdBoolean = false;
   this.secondBoolean = true;

   //do second thing logic
}

it's just that I have these boolean resets at the top of every method so I thought it would be cleaner to put all the resets in one method. Once I do though, it stops working.

Final Edit: As is often the case with weird errors like this, it was totally my fault! One of the divs with a different ngIf was not closed so turning that false made everything disappear! Thanks everyone for the help!

AylaWinters
  • 1,121
  • 2
  • 7
  • 24
  • 1
    What does console.log(this.secondBoolean) print? Where do you use this, in your HTML template? – Hypenate Jan 18 '22 at 21:36
  • 2
    You'll have to provide more code because this should work. – Mathew Berg Jan 18 '22 at 22:22
  • @Hypenate, Good question. It is in fact true, but it is just that it is not triggering the NgIf in the html. Any idea why not? – AylaWinters Jan 18 '22 at 23:16
  • 2
    How is this wired inside your template? Can you share your .html? Are you sure it is not nested below a falsy ngIf? – Lorraine R. Jan 18 '22 at 23:51
  • 1
    You could also check the value in your template {{secondBoolean}}. But it's trival and should of course just work. The only thing I can think of you made a mistake in the HTML (which we do not see) or you have set ChangeDetectionStrategy to OnPush – Hypenate Jan 19 '22 at 06:27
  • @RonEvans , @ Hypenate , I edited my question to show some code. It isn't the HTML because I didn't even open the HTML file when switching from the second code to the first code. I will look up the ChangeDetectionStrategy, though I think it is weird that it picks up changes on all the other button clicking, but not if there is a reset method... – AylaWinters Jan 19 '22 at 15:47
  • Console log in your reset, it's might be called extra times? – Mathew Berg Jan 19 '22 at 15:53
  • 1
    @RonEvans , @ Hypenate, Thank you so much. You were right, it was the HTML. I didn't have a closing div for one of the above ngIfs... I'm dumb but thanks for helping me solve it! – AylaWinters Jan 19 '22 at 16:40

0 Answers0