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;
}
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With