简体   繁体   中英

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

在过滤器表达式周围使用圆括号:

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

In Angularjs | is used for applying filters.Try this

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM