最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to access parent element and remove it with angularJs? - Stack Overflow

programmeradmin0浏览0评论

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();
发布评论

评论列表(0)

  1. 暂无评论