如何在创建过程中操纵更改ngRepeat数据?

Can*_*hit 2 angularjs angularjs-directive angularjs-scope

我有一个tableobject's创建时间戳我想这个时间戳转换为可读格式之前,ngRepeat它呈现的table.

我写了一个函数getDate做这个转换,但我无法找到调用这个方法function里面的ngRepeat

我的完整代码 - Plunker

HTML

  <body ng-controller='websitesCtrl'>
    <table >
    <th>Site name</th>
    <th>Creation date</th>
    <tr ng-repeat="website in websites">
        <td>
            <a href="/websites/{{website.id}}">{{website.name}}</a>
        </td>
        <td>
            <div>{{website.created}}</div>
        </td>
    </tr>
   </table>

   <h3>example of the working time convertion:</h3>
    </div>{{date}}<div>
Run Code Online (Sandbox Code Playgroud)

//controllers

webApp.controller ('websitesCtrl', function ($scope, Websites) {

    $scope.websites  = Websites.get();

    $scope.date = Websites.getDate( $scope.websites[0].created);



});


//services

webApp.factory('Websites', function(){

    var websites = {};

    websites.get = function() {
        return [{
            id: '1',
            created: 1391581344623,
            updated: '222212',
            name: 'google.com',
            secretKey: 'dhsd#22%$',
            publicKey: '234233@@@',
            userIdentification:'COOKIES'
          },
          {
            id: '2',
            created: 1392585542545,
            updated: '444412',
            name: 'walla.com',
            secretKey: 'dhsd#22%$',
            publicKey: '234233@@@',
            userIdentification:'NONE-COOKIES'
          },
                    {
            id: '3',
            created: 1393564362663,
            updated: '444412',
            name: 'Umms.com',
            secretKey: 'dhsd#22%$',
            publicKey: '234233@@@',
            userIdentification:'NONE-COOKIES'
          }

        ]
    };

   //getDate returns accurate date
    websites.getDate = function(timeStamp) {
        var monthNames = ["JANUARY", "FEBRUARY", "MARCH", "APRIL",
                          "MAY", "JUNE", "JULY", "AUGUST",
                          "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"];

        var date = new Date(timeStamp);
        // month part from the timestamp
        var month = date.getMonth();
        // day part from the timestamp
        var day = date.getDate();
        // year part from the timestamp
        var year = date.getFullYear();

        // will return date in MAY 2, 2013 format
        return monthNames[month] + ' ' + day + ',' + year;
    };

    return websites;

});
Run Code Online (Sandbox Code Playgroud)

Ufu*_*arı 5

你应该使用日期过滤器.它已经内置了AngularJS,不要重新发明轮子.

<div>{{website.created | date}}</div>
Run Code Online (Sandbox Code Playgroud)

这是plnkr的背面.您可能想要更改日期格式.