0

I was given a project developed by some other developer. He is no more working in this company. When I was going through the code, I saw one line of angular code.

*ngIf=" 0 && test1>120 && test2<900"

can someone tell me why 0 is included in this condition? what is the use of this type of statement?

  • 2
    Looks like a dead template. `0` will always be falsy with any number of `&&` – Suraj Rao Jul 01 '22 at 05:25
  • Either it was generated or the author has written it in a way to easily compose expressions and build it dynamically. Similar strategy to this except this was to be disabled: https://stackoverflow.com/q/242822 – Jeff Mercado Jul 01 '22 at 05:29
  • I imagine that is a "test": In anytime, someone want to check a component without this div and forgot remove after make the test. E.g. you can check something in code and write `if (false && condition){...}..others instructions..` to be sure not execute the code under the div. If you forget after make the test remove the false, your code never work property. Well, this can be about a part not yet implemented/checked and instead enclosed by comments use a *ngIf="false". – Eliseo Jul 01 '22 at 07:22

1 Answers1

2

*ngIf="0" is equivalent to *ngIf="false".

So, 0 && test1>120 && test2<900 will evaluate to false.

The *ngIf was probably added to hide dead or experimental code.

  • ok. but here is what I understand that condition wants to check whether test1>120 and test2<900. if both are true then the rest of the code work. then by including 0, this will be always false? I am right? pls, comment ... – Abhishek Mishra Jul 01 '22 at 06:37
  • 1
    Yes, if `test1>120 && test2<900` evaluates to `true`, adding a `&& 0` will make the entire statement `false`. The `*ngIf` was probably added to hide dead or experimental code. – Brian Bianchi Jul 02 '22 at 04:53
  • Thank you @brian-bianchi. This was for hiding one div for some reason from the live code. – Abhishek Mishra Jan 27 '23 at 09:47