Reputation: 401
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?
Upvotes: 13
Views: 99875
Reputation: 5826
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>
Upvotes: 24
Reputation: 131
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();
};
});
Upvotes: 1
Reputation: 783
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
Upvotes: 11
Reputation: 5054
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!
Upvotes: 16