gpt4 book ai didi

javascript - 如何将 {{data.date}} 与 timeago 一起使用? Angular

转载 作者:行者123 更新时间:2023-11-30 16:36:08 26 4
gpt4 key购买 nike

我正在尝试将 timeago.js 与 AngularJS 一起使用。如果我把日期放在缩写标题上,它就可以工作但是,如果我改用 {{data.commented_at}},它就不起作用。我正在 document.ready jquery 函数加载时间。

我该如何解决?

谢谢

最佳答案

这个 jsfiddle 有自己的 timeago 过滤器,所以你不必包含 jquery 库,而只需使用这个过滤器。

http://jsfiddle.net/i_woody/cnL5T/

过滤代码:

angular.module('yourmodule', []).filter('timeago', function() {
return function(input, p_allowFuture) {
var substitute = function (stringOrFunction, number, strings) {
var string = (typeof stringOrFunction === "function") ? stringOrFunction(number, dateDifference) : stringOrFunction;
var value = (strings.numbers && strings.numbers[number]) || number;
return string.replace(/%d/i, value);
},
nowTime = (new Date()).getTime(),
date = (new Date(input)).getTime(),
//refreshMillis= 6e4, //A minute
allowFuture = p_allowFuture || false,
strings= {
prefixAgo: null,
prefixFromNow: null,
suffixAgo: "ago",
suffixFromNow: "from now",
seconds: "less than a minute",
minute: "about a minute",
minutes: "%d minutes",
hour: "about an hour",
hours: "about %d hours",
day: "a day",
days: "%d days",
month: "about a month",
months: "%d months",
year: "about a year",
years: "%d years"
},
dateDifference = nowTime - date,
words,
seconds = Math.abs(dateDifference) / 1000,
minutes = seconds / 60,
hours = minutes / 60,
days = hours / 24,
years = days / 365,
separator = strings.wordSeparator === undefined ? " " : strings.wordSeparator,

// var strings = this.settings.strings;
prefix = strings.prefixAgo,
suffix = strings.suffixAgo;

if (allowFuture) {
if (dateDifference < 0) {
prefix = strings.prefixFromNow;
suffix = strings.suffixFromNow;
}
}

words = seconds < 45 && substitute(strings.seconds, Math.round(seconds), strings) ||
seconds < 90 && substitute(strings.minute, 1, strings) ||
minutes < 45 && substitute(strings.minutes, Math.round(minutes), strings) ||
minutes < 90 && substitute(strings.hour, 1, strings) ||
hours < 24 && substitute(strings.hours, Math.round(hours), strings) ||
hours < 42 && substitute(strings.day, 1, strings) ||
days < 30 && substitute(strings.days, Math.round(days), strings) ||
days < 45 && substitute(strings.month, 1, strings) ||
days < 365 && substitute(strings.months, Math.round(days / 30), strings) ||
years < 1.5 && substitute(strings.year, 1, strings) ||
substitute(strings.years, Math.round(years), strings);

return [prefix, words, suffix].join(separator).trim();
// conditional based on optional argument
// if (somethingElse) {
// out = out.toUpperCase();
// }
// return out;
}
});

关于javascript - 如何将 {{data.date}} 与 timeago 一起使用? Angular ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32672927/

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