0

I am working on a AngularJS web application and sometimes I need to check if the element inside the ng-if or ng-show directive is inside a list. I'm doing this now:

<div ng-if="object.element=='A' || object.element=='B' || object.element=='C'">
    <p>Hello World!</p>
</div>

I was wondering if there is a way to do something like this:

<div ng-if="object.element in ['A','B','C']">
    <p>Hello World!</p>
</div>
lborgav
  • 2,371
  • 19
  • 28

2 Answers2

2

You could do this:

<div ng-if="['A','B','C'].indexOf(object.element)>-1">
    <p>Hello World!</p>
</div>

Or this (it's the same):

<div ng-if="['A','B','C'].indexOf(object.element)+1">
    <p>Hello World!</p>
</div>

Working example

Josep
  • 12,926
  • 2
  • 42
  • 45
2

Try this

HTML

<div ng-if="HideShow()">
    <p>Hello World!</p>
</div>

JS

    $scope.object = {
        element: 'A'
    };

   $scope.HideShow = function () {
        return $scope.object.element == 'A' || $scope.object.element == 'B' || $scope.object.element == 'C'
    }
Shohel
  • 3,886
  • 4
  • 38
  • 75