prozaek.prozaek prozaek.prozaek - 1 month ago 5
AngularJS Question

Why does not the filter?

Please help fix the script.

html:

<li ng-repeat="date in dateArr | dateFormatter}">
<span class="date">{{date}}</span>
</li>


js:

angular.module('App', [])
.controller('lsController', function ($scope) {
$scope.dateArr = [
'10.10.2016',
'11.10.2016',
'12.10.2016',
'13.10.2016',
'14.10.2016',
'15.10.2016'
];

/*$scope.dateFormatter = function(date) {
return date.slice(0, 6);
}*/
})
.filter('dateFormatter', function (date) {
return date.slice(0, 6);
});


I use angular 1.4.8

JSFIDDLE

I need use filter, which cut the datestring

Answer

Your filter is not constructed properly.

A filter needs to return a function that contains the arguments for the filtering and returns the result

.filter('dateFormatter', function () {
      return function(dateString){
         return dateString.slice(0, 6); 
      }
});

Then you have this set to filter in the ng-repeat but putting it there it would need to return a filtered array, not a string input and manipulation. So it needs to be placed where you pass in a string

<li ng-repeat="date in dateArr">
        <span class="date">{{date | dateFormatter}}</span>
</li>

DEMO

Comments