185

I want to do so:

ng-hide="!globals.isAdmin && mapping.is_default"

but the expression evaluates always to false.

I do not want to define special function on $scope.

Paul
  • 25,812
  • 38
  • 124
  • 247
  • 1
    That syntax works for me, and I use it frequently. If it's evaluating to `false`, you might want to double-check those values. It's possible that the "globals" and/or "mapping" objects are `undefined` – derrylwc Mar 26 '15 at 22:50
  • Read my comment below the answer. – Paul Mar 27 '15 at 12:55
  • just a hint - if you use controller method instead, you can actually step through the evaluation in debugger! – Dimitry K Nov 30 '16 at 16:23

5 Answers5

216

Use a controller method if you need to run arbitrary JavaScript code, or you could define a filter that returned true or false.

I just tested (should have done that first), and something like ng-show="!a && b" worked as expected.

isherwood
  • 58,414
  • 16
  • 114
  • 157
Mark Rajcok
  • 362,217
  • 114
  • 495
  • 492
  • 4
    You are right. The problem was that the isAdmin flag was of type 'string' rather than 'boolean'. – Paul Mar 01 '13 at 21:18
124

ng-show / ng-hide accepts only boolean values.

For complex expressions it is good to use controller and scope to avoid complications.

Below one will work (It is not very complex expression)

ng-show="User=='admin' || User=='teacher'"

Here element will be shown in UI when any of the two condition return true (OR operation).

Like this you can use any expressions.

RAS
  • 8,100
  • 16
  • 64
  • 86
myaseedk
  • 1,948
  • 1
  • 14
  • 12
13

This will work if you do not have too many expressions.

Example: ng-show="form.type === 'Limited Company' || form.type === 'Limited Partnership'"

For any more expressions than this use a controller.

usumoio
  • 3,500
  • 6
  • 31
  • 57
lvadim01
  • 285
  • 3
  • 11
  • 1
    I dont think your statement is correct: `This will work if you do not have too many expressions.` although I agree that it should be done in the controller instead. – Rahul Desai Feb 10 '16 at 20:14
7

I generally try to avoid expressions with ng-show and ng-hide as they were designed as booleans, not conditionals. If I need both conditional and boolean logic, I prefer to put in the conditional logic using ng-if as the first check, then add in an additional check for the boolean logic with ng-show and ng-hide

Howerver, if you want to use a conditional for ng-show or ng-hide, here is a link with some examples: Conditional Display using ng-if, ng-show, ng-hide, ng-include, ng-switch

James Drinkard
  • 15,342
  • 16
  • 114
  • 137
2

Some of these above answers didn't work for me but this did. Just in case someone else has the same issue.

ng-show="column != 'vendorid' && column !='billingMonth'"
Deathstalker
  • 794
  • 10
  • 8