gpt4 book ai didi

angularjs - 如何在创建过程中操作更改 ngRepeat 数据?

转载 作者:行者123 更新时间:2023-12-02 04:51:40 25 4
gpt4 key购买 nike

我有一个,其中包含一个对象的创建时间戳我想将这个时间戳转换为ngRepeat 将其呈现到 table 之前的可读格式。

我写了一个函数 getDate 来做这个转换,但我找不到调用的方法ngRepeat

中的这个 函数

My complete code - 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>

Angular

//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;

});

最佳答案

你应该使用 date filter .已经内置了AngularJS,不要重新发明轮子。

<div>{{website.created | date}}</div>

这是 plnkr后退。您可能想要更改日期格式。

关于angularjs - 如何在创建过程中操作更改 ngRepeat 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18727647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com