Skip to content

Assign class to timeAgo directive base on value being a past or future value. #71

@gorgamore

Description

@gorgamore

I needed a way to style values based on them being past or future. Not sure if this is the correct way to do this but I found it very useful. Hopefully this information can help someone else who needs it.

link: function(scope, elem) {
      var fromTime;

      // Track changes to fromTime
      scope.$watch('fromTime', function(value) {
        fromTime = timeAgo.parse(scope.fromTime);
      });

      // Track changes to time difference
      scope.$watch(function() {
        return nowTime() - fromTime;
      }, function(value) {
            var elemHandle = angular.element(elem);
            elemHandle.text(timeAgo.inWords(value, fromTime, scope.format));

          if(fromTime < nowTime()){
              if(elemHandle.hasClass('time-ago-future')){
                  elemHandle.removeClass('time-ago-future');
              }
              if(!elemHandle.hasClass('time-ago-past')){
                  elemHandle.addClass('time-ago-past');
              }
          }else{
              if(elemHandle.hasClass('time-ago-past')){
                  elemHandle.removeClass('time-ago-past');
              }
              if(!elemHandle.hasClass('time-ago-future')){
                  elemHandle.addClass('time-ago-future');
              }
          }
      });
    }

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions