DeadManSpirit
DeadManSpirit

Reputation: 2096

AngularJS filter with ternary operator expression

I have the following code:

<td data-ng-repeat="fld in checkedFields" ng-init="field = result[fld.field]; filter = fld.filter">
<span>
    {{ null != filter ? field | filter : field }}
</span>
</td>

I am getting the following console error

Error: [$parse:syntax] Syntax Error: Token '|' is unexpected, expecting [:] at column 24 of the expression [null != filter ? field | filter : field] starting at [| filter : field].

Does any know how to use AngularJS filter with ternary operator expression?

NOTE: fld.filter will be a AngularJS filter

Upvotes: 3

Views: 2762

Answers (4)

gh9
gh9

Reputation: 10703

It is barfing on your | statement, that will not work, because | is a special char for angular (it means apply a filter) this is the ternary operator that will work if you are trying to do a regular ternary operator

{{null != filter ? filter : field}}

EDIT

If you want to apply the filter you will need to do it at the ng-repeat

<td data-ng-repeat="fld in checkedFields | filterExpression" ng-init="field = result[fld.field]; filter = fld.filter">
<span>
   {{fld.someProperty}}
</span>
</td>

you cannot apply your filter on the text you are displaying, and expect the filter to apply to the list. If you are trying to filter you list you will need to modify your code, to apply the filter at the ng-repeat. I have included a link on how filtering works, I recommend looking it over to get a handle on what is going on.

Upvotes: 0

user5621153
user5621153

Reputation:

{{ null != filter ? (field || filter) : field }}

Upvotes: 0

Wasiq Muhammad
Wasiq Muhammad

Reputation: 3118

In Angularjs | is used for applying filters.Try this

{{ null != filter ? (field | filter) : field }}

Upvotes: 2

Tarun Dugar
Tarun Dugar

Reputation: 8971

Use round brackets around the filter expression:

{{ null != filter ? (field | filter) : field }}

Upvotes: 8

Related Questions