[英]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.我想从ng-click
事件的目标元素访问父元素并将其删除。
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. $event.target
将给出 DOM 元素。 To use parent()
on it, it need to be wrapped as follow要在其上使用parent()
,需要按如下方式包装
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.