0

How can I test if a value exists in a map in a ng-if?

$scope.textInputTypes = {
      "currency": true,
      "double": true,
      "percent": true,
      "int": true,
      "email": true,
      "phone": true,
      "string": true,
      "textarea": true,
      "url": true
    };

ng-if='mytype in textInputTypes'

This gives me an error:

Syntax Error: Token 'in' is an unexpected token at column 17 of the expression [mytypeNaNn textInputTypes] starting at [in textInputTypes].

angelokh
  • 9,426
  • 9
  • 69
  • 139
  • You shouldn't need to check if the key exists. treat it like a boolean `if` with the benefit that angular won't throw exception if it doesn't exist – charlietfl Jul 24 '15 at 22:59

2 Answers2

0

You can also do the following:

<element ng-if='textInputTypes.hasOwnProperty(mytype)'>

Just keep in mind that the content inside the element won't be part of the DOM if the IF condition is not met, if you still want the content to exist you can also use ng-show

Walter R
  • 523
  • 1
  • 7
  • 10
0

Finally, I found a way to do it.

ng-if="textInputTypes[myType]"

angelokh
  • 9,426
  • 9
  • 69
  • 139