Shivaraj
Shivaraj

Reputation: 613

Dom manipulation in AngularJs

I wanted to know, If we want to manipulate the DOM. Where we should do that. Is it in a Controller or a directive or somewhere else ? I have read somewhere that manipulating the DOM in controller should be avoided. Is it correct?

Upvotes: 1

Views: 257

Answers (2)

Nidhish Krishnan
Nidhish Krishnan

Reputation: 20741

You should use Angular JS Directive for DOM manipulations. DOM Manipulations should not exist in Controllers, Services or anywhere else but in Directives.

One of the nicer features of AngularJS is the framework's ability to separate the roles of the model, the view, and the controller. The separation is clean enough that you shouldn't need to manipulate the DOM directly from code inside the controller. Instead, the controller only manipulates the model, which influences the view through data bindings and directives. The end result is a clean and testable.

Take a look at this

Video - This video tutorial covers manipulating the DOM in AngularJS using directives with a link function.

Upvotes: 1

Amir Popovich
Amir Popovich

Reputation: 29836

Best practice is:
Dom manipulations only in directives.

Read here

Upvotes: 1

Related Questions