I have an ng-repeat
element which will loop through $http.get()
result.
<tr ng-repeat="blog in posts">
<td style="text-align:center">{{ $index+1 }}</td>
<td>{{ blog.title }}</td>
<td>
{{ blog.author.name }}
</td>
<td>
{{ blog.created_at | date:'MMM-dd-yyyy' }}
</td>
</tr>
I have created_at
as timestamp
in MySQL database table. And I am using angular.js v1.0.7
.
I am getting the same output from db table and date filter is not working. How can I solve this?
My ajax call,
$http({method: 'GET', url: 'http://localhost/app/blogs'}).
success(function(data, status, headers, config) {
$scope.posts = data.posts;
}).
error(function(data, status, headers, config) {
$scope.posts = [];
});
The date passed to the filter needs to be of type javascript Date.
Have you checked what the value blog.created_at
is displayed as without the filter?
You said your backed service is returning a string representing the date. You can resolve this in two ways:
You can write your own filter as follows:
app.filter('myDateFormat', function myDateFormat($filter){
return function(text){
var tempdate= new Date(text.replace(/-/g,"/"));
return $filter('date')(tempdate, "MMM-dd-yyyy");
}
});
And use it like this in your template:
<td>
{{ blog.created_at | myDateFormat }}
</td>
Rather than looping through the returned array and then applying the filter