2015-09-19 21 views
1

我正在尝试在AngularJS中使用timeago.js。 如果我把它的日期放在缩写标题 但是,如果我使用{{data.commented_at}},它不起作用。 我正在加载timeago在document.ready jquery函数。如何在timeago中使用{{data.date}}? Angular

我该如何解决?

感谢

+0

它认为不好的做法,大部分使用的角度和jQuery一起,但也有例外。但也请提供一些代码,告诉我们你已经尝试了什么。 JSFiddle总是很好。如果你在你的问题上付出更多的努力,你会得到更好的回应。 –

+0

有timeago的角度模块,在angular app中使用'document.ready'是相当无用的。任何jQuery插件都应该在指令 – charlietfl

回答

1

这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; 
     } 
    }); 
+0

仍然有jQuery的依赖...它没有抱怨,但只是提到它 – charlietfl

+0

编辑和删除jQuery的依赖关系。感谢您指出。 –

1

TIMEAGO是一个jQuery插件,果然有一类TIMEAGO的和ISO 8601的时间戳的标题是这样的“4分钟前”或“大约1天前”全部缩写元素。

所以它需要你的HTML元素与类timeago缩写。 我希望你跟着这个角度。 因此,与角应该是::

<abbr class="timeago" title="commented_at">December 17, 2011</abbr> 

commented_at应该是范围的属性。

+0

中初始化,是的。正如我所说,没有角绑定,它工作正常。但是当我把{{commented_at}},它不工作 – harleydavi