Can I use filters inside ngIf statement

2020-03-24 07:35发布

问题:

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

回答1:

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.