3

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thought about that, however controller is now responsible for formatting data not veiw and that breaks some principles and stuff :P And peolpe generally dont like that. Not to mention big if constructs.
Within the constraints of a best practices, you could move the business logic into a service. And if you have a lot of conditionals this makes a lot of sense too.
0

Angular already has that built-in. Just use something like :

<td class="table-details-panels">{{info.value | date: 'dd-MM-yyy'}}</td>

If you only want to filter some td's :

<td ng-if="needsToBeFilteredCondition" class="table-details-panels">{{info.value | date: 'dd-MM-yyyy'}}</td>
<td ng-if="!needsToBeFilteredCondition" class="table-details-panels">{{info.value}}</td>

1 Comment

The thing is I dont want to attach same filter to all fields from table, I will add more code to clarify

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.