Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8940715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:06:40+00:00 2026-06-15T11:06:40+00:00

I’m trying to implement a simple directive based on the jQuery timeago plugin. Here

  • 0

I’m trying to implement a simple directive based on the jQuery timeago plugin. Here is the code for the directive (well, as far as I’ve gotten so far)

<small timeago milliseconds="{{conversation.timestamp}}"></small>

I am trying to use the timestamp (in milliseconds), and let angularJs bind the timeago() function like this..

App.Directives.directive('timeago', function() {

    return {
        restrict: 'A',
        replace: false,
        scope: false,
        link: function (scope, element, attrs) {

            scope.$watch('ready', function () {

                var x = attrs['milliseconds'];

                alert(x);

                $(element).timeago();

            });
        },
    };

});

It works just fine when I manually set the value of milliseconds, but it seems the $scope hasn’t done it’s thing yet… I’m sure this is something simple, I just don’t know the right words to google it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:06:41+00:00Added an answer on June 15, 2026 at 11:06 am

    I’m not sure that scope.$watch does what you are expecting it to do; scope.$watch takes as its first argument an expression to evaluate on the current scope; when that expression returns a new value, it will call the second argument, a function, with the new value. Thus,

    scope.$watch('ready', function() {...});
    

    is basically the same as saying

    Call this function every time scope.ready changes.

    which is obviously not what you want.


    On to your functionality–there are a few ways you might go about implementing something like this. The first is a simple filter:

    app.filter('timeago', function() {
      return function(time) {
        if(time) return jQuery.timeago(time);
        else return "";
      };
    });
    
    <p>The timestapm was {{conversation.timestamp|timeago}} ago.</p>
    

    In this case, however, the returned string would automatically refresh any time a digest cycle is run on the scope.

    To only process the timestamp exactly once, you might use a directive like the following:

    app.directive('timeago', function($timeout) {
      return {
        restrict: 'A',
        link: function(scope, elem, attrs) {
          scope.$watch(attrs.timeago, function(value) {                
            if(value) elem.text(jQuery.timeago(value));
          });
        }
      };
    });
    
    <p>The timestamp was <span timeago="conversation.timestamp"></span> ago.</p>
    

    Here is a version that re-runs a digest cycle every 15 seconds, to automatically update the timestamp every so often:

    app.directive('timeago', function($timeout) {
      return {
        restrict: 'A',
        link: function(scope, elem, attrs) {
          var updateTime = function() {
            if (attrs.timeagoAutoupdate) {
              var time = scope.$eval(attrs.timeagoAutoupdate);
              elem.text(jQuery.timeago(time));
              $timeout(updateTime, 15000);
            }
          };
          scope.$watch(attrs.timeago, updateTime);
        }
      };
    });
    
    <p>The timestamp was <span timeago="conversation.timestamp"></span> ago.</p>
    

    Here is a jsFiddle that demonstrates all three examples. Do note that the only reason the third example (with the filter) is automatically updating every minute is becaues the second example (the timeagoAutoupdate directive) is calling scope.$eval.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am reading a book about Javascript and jQuery and using one of the
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.