Use jQuery timeago or momentjs and AngularJS together

前端 未结 4 1688
迷失自我
迷失自我 2020-12-23 12:48

I want to use timeago plugin to make dates look nicer. The problem is that these dates are fetched via AngularJS from the REST dynamically. So, when I attach this jQuery plu

4条回答
  •  死守一世寂寞
    2020-12-23 13:35

    moment.js is the best choice. I have created a generic moment filter that allow you to use any moment formatting method.

    angular.module('myApp', [])
      .filter('moment', [
        function () {
          return function (date, method) {
            var momented = moment(date);
            return momented[method].apply(momented, Array.prototype.slice.call(arguments, 2));
          };
        }
      ]);
    

    Use it like

    {{ date | moment:'fromNow' }}
    {{ date | moment:'calendar' }}

    You can checkout it in action here http://jsfiddle.net/gregwym/7207osvw/

提交回复
热议问题