Reputation: 803
I am new to AngularJs. I am having a div section and some jquery code related to the div elements. I am planning to make a partial of this code and create a custom directive. Is this is good design. Also does angular gives such flexibility to include pure jquery code inside a angular directive?
Upvotes: 0
Views: 91
Reputation: 827
Having Jquery code in controllers creates following issues:
1) Memory leaks.
2) Dom Manipulations should not exist in controllers, services or anywhere else but in directives.
3) DOM manipulation is outside of the Angular scope – so if there were any calls to the backend, you will need to add an additional step of appending that information to the book list array.
So, it is good practice to write a directive to write Jquery code.
Refer Best Practice - Dom Manipulations
Upvotes: 0
Reputation: 8646
Having jQuery code in your viewcontrollers is smelly.
Using a directives to apply jquery code to elements, is the best approach. This logic should be isolated from the controller, to possibly apply the directive to many elements.
If you need to communicate from directive to viewcontroller, something is smelly again. (callbacks are allowed)
And jQuery is a global variable, so yeah you can use jquery in that directives.
The link
und controller
function do pass the $element
parameter, which is the reference to the html element this directive is applied to. This $element
variable is already wrapped with jquery. so you can do $element.attr("...")
or something.
Upvotes: 1