简体   繁体   中英

How to access parent element and remove it with angularJs?

I want access to parent element from ng-click event's target element and remove it.

I looked some pages and angular docs and found something like below but this is not worked for me.

My Template:

<div class="element-which-i-want-access">
   <span>
      <button ng-click="remove(myModelObjectInCurrentScope, $event)" class="btn btn-sm btn-danger"><i class="fa fa-trash"></i></button>
   </span>
</div>

My Controller:

$scope.remove = function(object, $event) {
   var el = $event.target; // this meaning as clicked <button> element 
   var myTargetElement = el.parent().parent(); // this not working
   myTargetElement.remove(); // i couldn't tried this step but i couldn't got parent element yet
}

How can I do this? Thanks in advance.

$event.target will give the DOM element. To use parent() on it, it need to be wrapped as follow

angular.element(el).parent().parent();

accepted answer did'nt work for me. but this code did:

var elem= angular.element($event.currentTarget);
var parent = elem.parent().parent();
parent.remove();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM