Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In AngularJS, how can I remove an element created using $compile?

I've written a simple example, where I create an element dynamically using $compile. This new element has another button, with which I want to remove this element (I've read that it is good to destroy scope/elements to avoid leaks). But the function closeThisElement() is not working; please help.

See plunker here: http://plnkr.co/edit/js7mpUMndjZEdMvRMuZk?p=preview

Also reproducing part of the code below:

link: function($scope, $element) {
    function closeThisElement(){
        $element.remove();
    }
    function addComment(){
        $element.append($compile('<div class="publishComment"><input type="text" ng-model="contentForReply"/><button ng-click="publishReply(contentForReply); closeThisElement()">Publish Reply</button></div>')($scope));
    }
    $scope.addComment = addComment;
}
like image 493
Kaya Toast Avatar asked Mar 28 '14 19:03

Kaya Toast


People also ask

How to remove DOM element in AngularJS?

Here the task is to remove a particular element from the DOM with the help of AngularJS. Approach: Here first we select the element that we want to remove. Then we use remove() method to remove that particular element. Example 1: Here the element of class('p') has been removed.

How AngularJS is compiled?

AngularJS compilation process takes place in the Web Browser. No Server side or pre-compilation step is involved. Angular uses $compiler service to compile your Angular HTML page. The Angular compilation process begins after your HTML page (static DOM) is fully loaded.

What is compile and link in AngularJS?

Compile function: It is used for template DOM Manipulation and collect all of the directives. Link function: It is used for registering DOM listeners as well as instance DOM manipulation. It is executed once the template has been cloned.

What is Prelink and Postlink in AngularJS?

Pre-linking function Executed before the child elements are linked. Not safe to do DOM transformation since the compiler linking function will fail to locate the correct elements for linking. Post-linking function Executed after the child elements are linked.


2 Answers

They have to be on the $scope. Currently they are just functions available to your link, but not within the html. Try this.

$scope.closeThisElement = closeThisElement;

To eliminate only the compiled component, save the instance and use that.

link: function($scope, $element) {
    var newElement;
    function closeThisElement(){
        newElement.remove();
    }
    function addComment(){
        newElement = $compile('<div class="publishComment"><input type="text" ng-model="contentForReply"/><button ng-click="publishReply(contentForReply); closeThisElement()">Publish Reply</button></div>')($scope);
        $element.append(newElement);
    }
    $scope.addComment = addComment;
    $scope.closeThisElement = closeThisElement;
}

It might be worth mentioning, that rather than creating and removing a new element, you could use ng-show or ng-hide and never have to compile it.

like image 167
Zack Argyle Avatar answered Oct 06 '22 08:10

Zack Argyle


The function closeThisElement needs to be part of the $scope for it to evaluate in your HTML:

$scope.closeThisElement = closeThisElement;

Though, the $element in Angular's link function refers to the directive element itself.

$element.remove() will remove the directive from the DOM. You can use angular.element() (alias to jQuery lite) to find the appended content and remove that to keep the tk-listview element.

like image 39
johntayl Avatar answered Oct 06 '22 07:10

johntayl