Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

element.ready vs angular.element($document).ready

I am working on a directive but I don't want to play with $document or $window, only with the element itself.

Before I had:

angular.element($document).ready(function() {...});

and was working, just a few minutes ago I changed, and put:

element.ready(function() {...});

and it is working also.

So, when I say element.ready(function() {...}) am I telling Angular to run that function when the element <my-directive></my-directive> is ready? or what am I doing?

I am asking this because I am wondering, why it is still working if I do element.ready instead.

like image 683
Non Avatar asked Sep 13 '25 09:09

Non


1 Answers

You don't need either.

ready isn't needed since element has to exist for link function to fire. Also there is no element level ready event ... it is only used at document level to account for full body of page existing. That stage is long over when angular is compiling directives.

You can do any manipulation or event binding directly to element immediately within link function of directive

like image 52
charlietfl Avatar answered Sep 14 '25 21:09

charlietfl