javascript - element.ready vs angular.element($document).ready - Stack Overflow

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

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.

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.

Share Improve this question asked Jul 13, 2015 at 22:20 NonNon 8,61920 gold badges80 silver badges130 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 6

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 piling directives.

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

In angularjs, angular.element is a jqLite object

And element in angular directive is a jqLite object and you dont need to wrap again with angular.element

for example in this code all elements is same

var element1 = angular.element( document.getElementById('test') ); // a jqLite object

var element2 = angular.element( element1 ); // same jqLite object

var element3 = angular.element( element2 ); // same jqLite object

By the way you don't need ready because directives links call when element ready

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744961829a4603434.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信