I have an object with 100+ attributes, such as "name", "price", "expiry date"...etc I am using ng-repeat to iterate through all the key-pair values of the object and displaying them on a table.
<table class="table">
<tr ng-repeat="x in attr_array">
<td><b>{{x.key}}</b></td>
<td>{{x.value}}</td>
</tr>
</table>
But I want to use the Angular date-filter on certain attributes, such as any date fields:
{{ x.value | date: 'MMM d, y'}}
And ideally other filters too. How can I go about doing this?
I tried to recreate your problem and solved it with ng-if
.
There seems to be a function in the angular namespace to check every type like date, string, number, which I injected into the view through the scope.
Also notice I used the ng-repeat="(key, value) in ..."
assuming that you are iterating over an object, source.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body ng-app="app" ng-controller="RootController">
<table>
<tr ng-repeat="(key, value) in attr_array">
<td><b>{{key}}</b>
</td>
<td>
<span ng-if="isDate(value)">{{value | date: 'MMM d, y'}}</span>
<span ng-if="isNumber(value)">{{value | number: 4}}</span>
<span ng-if="isString(value)">{{value | uppercase}}</span>
</td>
</tr>
</table>
<script>
angular.module('app', [])
.controller('RootController', function($scope) {
$scope.isDate = angular.isDate;
$scope.isNumber = angular.isNumber;
$scope.isString = angular.isString;
$scope.attr_array = {
date: new Date(),
str: "hello",
nm: 50.2
};
});
</script>
</body>
</html>