Use jQuery timeago or momentjs and AngularJS together

前端 未结 4 1704
迷失自我
迷失自我 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:54

    If you need jQuery, writing a directive/filter is the way to go.

    app.directive("timeAgo", function($compile) {
      return {  
        restrict: "C",
        link: function(scope, element, attrs) {
          jQuery(element).timeago();
        }
      };
    });
    
    app.filter("timeAgo", function() {
      return function(date) {
        return jQuery.timeago(date); 
      };
    });
    

    Directive and/or Filter (jsbin)

提交回复
热议问题