如何在 timeago 中使用 {{data.date}}? Angular
how to use {{data.date}} with timeago? Angular
我正在尝试将 timeago.js 与 AngularJS 一起使用。
如果我把日期放在缩写标题上,它就可以工作
但是,如果我改用 {{data.commented_at}} ,它就不起作用。
我正在 document.ready jquery 函数加载时间。
我该如何解决?
谢谢
Timeago 是一个 jQuery 插件,它将标题中带有 class timeago 和 ISO 8601 时间戳的所有缩写元素转换为类似“4 分钟前”或 "about 1 day ago".
所以它要求您的 html 元素与 class 时间之前是缩写。
我希望你用 angular 关注这个。
所以 angular 应该是 ::
<abbr class="timeago" title="commented_at">December 17, 2011</abbr>
commented_at 应该是 属性 范围。
这个 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;
}
});
我正在尝试将 timeago.js 与 AngularJS 一起使用。 如果我把日期放在缩写标题上,它就可以工作 但是,如果我改用 {{data.commented_at}} ,它就不起作用。 我正在 document.ready jquery 函数加载时间。
我该如何解决?
谢谢
Timeago 是一个 jQuery 插件,它将标题中带有 class timeago 和 ISO 8601 时间戳的所有缩写元素转换为类似“4 分钟前”或 "about 1 day ago".
所以它要求您的 html 元素与 class 时间之前是缩写。 我希望你用 angular 关注这个。 所以 angular 应该是 ::
<abbr class="timeago" title="commented_at">December 17, 2011</abbr>
commented_at 应该是 属性 范围。
这个 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;
}
});