ntl0ve
ntl0ve

Reputation: 1986

$log (or other services inside AngularJS directives)

I have the following directive I use to initialize the timeago plugin.

Directives.directive('timeago', function() {
   return function(scope, element, attrs) {
       $(element).attr('title', scope.post.utc_posted);
       $(element).timeago();
   }
});

How could I use/pass $log inside the function I'm returning?

Upvotes: 3

Views: 1696

Answers (1)

Michael Low
Michael Low

Reputation: 24506

You can just inject it the normal way. BTW element is already a jQuery variable and does not need $(element) - providing you're loading jQuery before Angular.

Directives.directive('timeago', function($log) {
   return {
    link: function(scope, element, attrs) {
       element.attr('title', scope.post.utc_posted);
       element.timeago();
     }
   }
});

Upvotes: 7

Related Questions