无法在AngularJS中更改响应对象中的日期格式

时间:2018-04-13 14:38:45

标签: angularjs

我无法在响应对象中更改日期格式

如何更改?

$http({
            method: 'GET',
            url: 'api/url'
        }).success( function (data) {
                $scope.reports = data.data;
                console.log($scope.reports);
                    $scope.reports = $scope.reports.map(obj =>{
                        obj.created_at  = new Date(obj.created_at);
                        return obj;
                    });
          }).error(function (response){
                console.log("error");  
       });

我尝试使用上面的代码更改日期格式

我得到了这个回复

{  
   "my2":[  
      {  
         "id":5,
         "cuid":20,
         "name":"my2",
         "month":"04",
         "created_at":"2018-04-01 00:00:00",
         "updated_at":"2018-04-11 00:00:00",
         "time":"04:32 PM",
         "status":"D"
      },
      {  
         "id":4,
         "cuid":20,
         "name":"my2",
         "month":"04",
         "created_at":"2018-04-02 00:00:00",
         "updated_at":"2018-04-12 00:00:00",
         "time":"12:10 PM",
         "status":"P"
      },
   ],
   "my":[  
      {  
         "id":44,
         "cuid":21,
         "name":"my",
         "month":"04",
         "created_at":"2018-04-12 00:00:00",
         "updated_at":"2018-04-12 00:00:00",
         "time":"09:08 PM",
         "status":"P"
      }
   ],
   "Testing":[  
      {  
         "id":43,
         "cuid":19,
         "name":"Testing",
         "month":"04",
         "created_at":"2018-04-12 00:00:00",
         "updated_at":"2018-04-12 00:00:00",
         "time":"09:05 PM",
         "status":"P"
      }
   ]
}

在html中我试试这个

{{data.created_at |日期:'dd'}}

但我收到了错误

TypeError:$ scope.reports.map不是函数

我认为因为它的对象所以这个问题

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我建议你使用angularjs做一个forEach(它处理双向绑定)

angular.forEach($scope.reports, function(report){
    report.createdAt = new Date(report.createdAt);
}