Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't change CSS style in AngularJS

Tags:

For some reasons, I can't change the style of an element using the following :

angular.element("#element").style.height = 100px; 

I'm sure that angular.element("#element") works, as it returns a DOM element. Plus, this works :

angular.element("#element").addClass('something'); 

(Everything I've found is about ngStyle but I don't think it is what I'm looking for ?)

Should I use something else ?

If so : What ? Why .style.something doesn't work ?

like image 248
Clément Malet Avatar asked Jun 23 '14 12:06

Clément Malet


1 Answers

According to the docs:

Note: all element references in Angular are always wrapped with jQuery or jqLite; they are never raw DOM references.

So the .style property is not available directly, since it is a property of the wrapped HTMLElement.


You can either use ivarni's approach to get hold of the HTMLElement (angular.element(...)[0].style...) or use jQuery's/jqLite's .css() method:

angular.element('#element').css('height', '100px'); 
like image 136
gkalpak Avatar answered Oct 01 '22 07:10

gkalpak