I have some JSON e.g.

  [{
    'name': 'test',
    'phone': '012345678',
    'date': '02/03/2014'
   },
   { ... }
  ]

A controller something like :

var app = angular.module('App', []);


app.controller('NameListCtrl', function($scope, $http) {

    $http.get('/api/v1/names.json').success(function(names) {
            $scope.names = names;
    });
});

HTML/JADE something like :

div#names(ng-controller="NameListCtrl")
   li.course_list(ng-repeat="name in names | orderBy:'date1'")
    span
     {{name.date}})                    // I want to do some formatting on this
    span
     {{name.name}}

I would like to perform a formatting operation on each date, is there a way of doing so without using filters ? I have seen something using the $httpProvider.

有帮助吗?

解决方案

A way to do it in the controller... insert your own filter func.

app.controller('NameListCtrl', function($scope, $http, $filter) {

    $http.get('/api/v1/names.json').success(function(names) {
          angular.forEach(names, function(item){
              item.date = $filter('date')(item.date, "shortDate");
          });
          $scope.names = names;
    }); 
 });

You could also use jade and do:

{{name.date|date:'shortDate'}})    
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top