Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular filter name as variable

I'm designing universal table that reads data and columns from ajax. In columns description is also filter name which angular should use for a specific column. But in HTML templates I can't use variables for filter names:/ Is there a solution for that? Or should I code javascript loop with data source?

Here is code example:

<tr ng-repeat="item in data">
    <td ng-repeat="col in cols">
        {{item[col.source]}}
        <span ng-if="col.ngFilter">
            {{col.ngFilter}} // ex. "state" filter
            {{item[col.source]|col.ngFilter}} //is not working - is looking for "col.ngFilter" not "state" filter.
        </span>
    </td>
</tr>
like image 644
piernik Avatar asked May 06 '26 18:05

piernik


1 Answers

You cannot do it in your HTML. First, you need to apply the filter in your controller.

function MyCtrl($scope, $filter) {

    $scope.applyFilter = function(model, filter) {
        return $filter(filter)(model);
    };

}

Then, in your HTML:

Instead of

{{item[col.source]|col.ngFilter}}

use

{{applyFilter(item[col.source], col.ngFilter)}}
like image 90
Kursad Gulseven Avatar answered May 09 '26 15:05

Kursad Gulseven



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!