Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I pass filter to html?

I have multiple fields in table that I am displaying simply with ng-repeat, but the need to format them occurred, is there some way to pass filter from scope to html? Below code doesn't work:

<tr ng-repeat="info in array">
     <td>{{info.key}}</td>
     <td class="table-detail-panels">{{ info.value | info.filter }}</td>
</tr>

aslo dont know if I should pass string or filter object

info.filter = 'date' // or
info.filter = $filter('date')

EDIT1: info.filter is a variable, it can have different filters, not only 'date'.

EDIT2: It is possibly worth to mention that ng-repeat is inside directive/controll that im building. And purpose of this controll is to being able to display everything that exist on this world in a sensible way, thats why I need filters. Or easy way of adding them to controll.

ANSWER: After browsing a while I found answer on stack: https://stackoverflow.com/questions/21491747/apply-formatting-filter-dynamically-in-a-ng-repeat

like image 519
Kmaczek Avatar asked Dec 18 '25 16:12

Kmaczek


1 Answers

From what I'm understanding, you want to filter a value in the array differently based off some conditions. If so, I'd recommend using the filter inside your controller and then repeating in HTML preformatted. And use ng-bind-HTML with ngsanitize if needed:

module.controller('MyCtrl', ['$scope', 'customFilter1', 'customFilter2',
    function ($scope, custom1, custom2) {

        // however you are creating your array - do the logic here
        if(something) {
           $scope.variable = custom1(arg)
        } else {
           $scope.variable = custom2(arg)
        }


     }
])

** This is the general idea, obviously it will not work exactly like written above. I use the same idea often when I'm not 100% sure what data will be passed and want to format strings, numbers differently.

like image 178
JSV Avatar answered Dec 20 '25 12:12

JSV



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!