Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
20
rated 0 times [  26] [ 6]  / answers: 1 / hits: 49024  / 11 Years ago, thu, may 23, 2013, 12:00:00

How to get body element in angular directive? my objective is to do what one do with jquery $('body').innerWidth(); inside directive. I do not want to use jquery but angular built-in jqlite implementation.


More From » angularjs

 Answers
16

If you need to access the body element from within a directive that is applied on another element, you can make use of the $document service like so..



app.directive(myDirective, function($document) {
return function(scope, element, attr) {
var bodyWidth = $document[0].body.clientWidth;
console.log(width of body is + bodyWidth);
};
});

<button my-directive>Sample Button</button>


You could also use the DOM traversal methods provided in jqLite (though they are much less powerful than what jQuery offers). For example, you could do a recursive lookup using the angular.element.parent() method to find the body tag.


[#78051] Wednesday, May 22, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kevonmoisesf

Total Points: 693
Total Questions: 101
Total Answers: 128

Location: Reunion
Member since Mon, Dec 28, 2020
3 Years ago
kevonmoisesf questions
Sat, Jan 23, 21, 00:00, 3 Years ago
Tue, Feb 18, 20, 00:00, 4 Years ago
Wed, Jun 12, 19, 00:00, 5 Years ago
;