Reputation: 648
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
Upvotes: 3
Views: 137
Reputation: 351
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.
Upvotes: 1
Reputation: 3591
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>
Upvotes: 0