prozaek.prozaek prozaek.prozaek - 3 months ago 14
AngularJS Question

angularjs : Why does the filter not work?

Please help fix the script.


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


angular.module('App', [])
.controller('lsController', function ($scope) {
$scope.dateArr = [

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

I use angular 1.4.8


I need use filter, which cut the datestring


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>