Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove HTML element on click in Angular js

Tags:

html

angularjs

This is my Directive. which display one Div on the body.

app.directive("autosuggest", function($rootScope) {
      return {
            scope: {
              doneFlag      : "=",
              groupFlag     : "=",
              inviteesFlag  : "=",
              init: '&'
            },
            templateUrl : "title.html",
            link: function(scope, element, attrs) { 
                  }

});

And in title.html

<div class="showw">
   <img id="hideDivOnClick" src="ddd.png"/>
</div>

And i include directive like this

<div autosuggest="" done-Flag="1" group-Flag="1"  invitees-Flag="1" selected-Array="" ></div>

so when i click on image then this <div autosuggest="" done-Flag="1" group-Flag="1" invitees-Flag="1" selected-Array="" ></div> parts gets remove from body. like remove element in Javascript. how i will achive this in angularJS?

like image 762
Proxy_Server Avatar asked Feb 06 '14 06:02

Proxy_Server


People also ask

How to remove html tag in Angular?

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.

How to remove html element in DOM?

HTML DOM Element remove() The remove() method removes an element (or node) from the document.

How do you delete an element in HTML?

Select the HTML element which need to remove. Use JavaScript remove() and removeChild() method to remove the element from the HTML document.

How to remove DOM element js?

Use the removeChild() or remove() method to remove an element from the DOM.


2 Answers

For clearing/removing html elements, we can use the following methods

var myEl = angular.element( document.querySelector( '#divID' ) );
myEl.empty();  //clears contents

var myEl = angular.element( document.querySelector( '#divID' ) );
myEl.remove();   //removes element

Reference : http://blog.sodhanalibrary.com/2014/08/empty-or-remove-element-using-angularjs.html#.Vd0_6vmqqkp

like image 107
Aparna Avatar answered Oct 02 '22 22:10

Aparna


Use below in your directive.

Directive

app.directive("removeMe", function() {
      return {
            link:function(scope,element,attrs)
            {
                element.bind("click",function() {
                    element.remove();
                });
            }
      }

});

HTML

<div class="showw" remove-me>
   <img id="hideDivOnClick" src="ddd.png"/>
</div>

Working Demo

like image 22
Jay Shukla Avatar answered Oct 02 '22 21:10

Jay Shukla