Search code examples
angularjsangular-filters

Can I use filters inside ngIf statement


Is there a way to use filter inside a ng-if statement ?

for example:

   <div ng-if="someVarString == ('someValue' | translate )">
        <span>Hello</span>
    </div>

Note: the translate filter returns a string

I know how to do it inside the controller, but I would like to use on the HTML


Solution

  • Yes that works. Here's a plunkr: http://plnkr.co/edit/vpCzutMnEFlTWC9gceU3?p=preview

    angular.module('plunker').filter('two', function() {
      return function (input) { return 2; }
    });
    

    Equivalent of your code:

    <div ng-if="2 == ('foobar' | two )">
      <span>Hello</span>
    </div>
    

    So your problem must be elsewhere.