2

In an angular directive I am making a clone of the element that I want which is

var node= element[0].cloneNode(true);

then I want to add css to this element. I know you can do this

$(node).css({width: width, left: left, height: height, top:'0px'});

but I know it is bad practice to use this in an angular directive. I have tried

angular.node.css({width: width, left: left, height: height, top:'0px'});

but this does not work. I have scoured the internet and have found nothing. All I get is jquery ways. Does anyone know how to add css to an element the angular way?

To any help, thanks!

4 Answers 4

7

Angular comes with jqLite, which is a trimmed down version of jQuery. There's nothing wrong with using it in a directive. clone() and css() are available in jqLite...

var node = element.clone()
                  .css({width: width, left: left, height: height, top:'0px'});

Working example: http://jsbin.com/yowubixa/1/edit?html,js,output

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @anthony, I thought this would work, but I still get a 'Uncaught TypeError: Cannot read property 'css' of undefined' error. Do you know what this could mean?
Can you post your code, maybe in a fiddle? I just tried this in the link function of a directive and it worked.
@user3339134 Updated answer with the example.
that is weird, it works on jsbin, but I am still getting 'css' of undefined. Weird. Thanks for your input!
What you said seems to work. Kind of. The reasons it might not work, may be on my part. In my directive when I print console.log(element[0].parentElement) I get the dom, but what I want is the array like thing that has children and offset. How do I print that info of a parent. By the way, since I think this works I am going to set this as the answer. Thanks!
0

ngStyle directive takes an "Expression which evals to an object whose keys are CSS style names and values are corresponding values for those CSS keys."

1 Comment

Thanks for responding @phylax. But, I am doing all the values in javascript/directive so I can't use ngStyle
0

To those who may find this later, I found the solution to my problem. The reason why I could not do node.css() was because node was a dom element. This was due to my copy. To keep the javascript object you have to do

angular.element(element[0]).clone() 

to properly clone it. this then allows me to do node.css(). You can do other things besides .css. You can look at the following here.

http://docs.angularjs.org/api/ng/function/angular.element

Hope this helps!

Comments

0

open ....component.js

angular.element(idName).css("background", "#000");

i.e hello is idName in div.

<div id="hello"></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.