Reputation: 721
I have a code that inserts a div
with content whenever the user clicks a button. What is the angularjs way to insert or remove elements from the DOM?
Upvotes: 0
Views: 116
Reputation: 461
You should always use a directive to manipulate DOM, especially if the data is from control
Upvotes: 0
Reputation: 364677
If the elements you want to add is somehow related to or near the button, use a directive. Normally all DOM manipulation is performed by a directive.
However, there seems to be an exception to this rule: if you want to insert something like a modal dialog box -- i.e., something that doesn't have to be placed somewhere specific -- you might want to use a service. Listen to a few minutes of Misko regarding this.
If you do want a dialog, see http://angular-ui.github.io/bootstrap/#/dialog and here is an SO answer that uses it in a plunker.
Upvotes: 2