6

I am trying to apply a specific class to an li element if 2 conditions are true, so I wrote the following code but it is not seem to be working

ng-class="{current:isActive('/'), loginStatus: false}"

Can someone please tell me what I am doing wrong and how to fix it so that the class is applied only if route is at / and loginstatus is false? Thanks

MChan
  • 6,842
  • 27
  • 83
  • 132

2 Answers2

9

The key should define the class you want, and the rest should be the expression to evaluate, which determines if the class is shown...

ng-class="{current:isActive('/') && loginStatus === false}"

What you were saying was more like... set class current if isActive('/') and also set class loginStatus if false is true.

Billy Moon
  • 57,113
  • 24
  • 136
  • 237
  • I'd recommend === instead of == – GonchuB Apr 05 '14 at 22:19
  • 5
    I think I remember that `===` became fashionable around the same time as the Gillette Mach III. Now there are razors with four blades, do you think javascript needs an `ulta equality` comparison symbol - `====`? – Billy Moon Apr 05 '14 at 22:24
  • @GonchuB - you are right of course - I changed it. But what about simply `&! loginStatus` - it is probably fine too no? Depends on what `loginStatus` can be. – Billy Moon Apr 05 '14 at 22:26
2

Right now it is set up to add the class current if isActive('/') return true, and the class loginStatus if false is true (which it, obviously, never will be).

What you need to do, to add the class current if isActive('/') AND loginStatus == false, is simply

ng-class="{current:isActive('/') && !loginStatus}"
dave
  • 62,300
  • 5
  • 72
  • 93