admin管理员组

文章数量:1319025

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.

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.

Share Improve this question edited Dec 28, 2020 at 12:38 Furkan Başaran asked Oct 31, 2015 at 11:21 Furkan BaşaranFurkan Başaran 1,9472 gold badges19 silver badges28 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 4

$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();

本文标签: javascriptHow to access parent element and remove it with angularJsStack Overflow