13

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?

4 Answers 4

24

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

Sign up to request clarification or add additional context in comments.

2 Comments

This will only remove the IMG element, but that is easily addressed.
Also, jquery is unnecessary. You can just use element.parent().remove() inside of the click handler.
16

You can simply create a directive, that adds a function that will remove the html of the element. Then you can just stick it on an ng-click. I made a fiddle here: http://jsfiddle.net/qDhT9/

// in the directive
scope.remove = function() {
    elt.html('');
};
// in the dom
<div remove-on-click ng-click="remove()"> 
    REMOVE ME 
</div>

Hope this helped!

4 Comments

Also @hassassin can we apply ng class to custom directive. Like <div id="autosuggest" class="autosuggest" ng-class="{'Show-Class':displayDivCss==true}" autosuggest="" done-Flag="1" group-Flag="1" invitees-Flag="1" selected-Array="" ></div>
sure, it will control the class on the div
this is not working. i was check condition also inside ng-class.but it will not apply show-class to div.so can you tell me other way to apply conditional class.
ng-class is correct. This is a different question than your original, but here is a working example jsfiddle.net/qDhT9/6
11

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

1 Comment

It used to work with Angular 1. Not sure if there are changes with newer versions. remove function should still be good I guess!
1

A more robust adaptation of Aparna's answer, you can use the $document service which acts as a wrapper for the browser's 'window.document' object. So instead of accessing 'document' variable globally you can access the '$document' object as a dependency. This helps to make your code more testable.

eg:

app.controller('exampleCtrl', function($scope, $document) {
  $scope.deleteArticle = function(id) {
    var articleRow = angular.element($document[0].querySelector('div[data-articleid="'+id+'"]'));
    articleRow.remove();
  };
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.