简体   繁体   English

如何在点击时删除父元素? - AngularJS

[英]How to remove parent element on click? - AngularJS

I have a button beside my list item which is to remove the list item. 我的列表项旁边有一个按钮,用于删除列表项。

My button is inside the list so I am trying to find a way to remove the parent element. 我的按钮在列表中,所以我试图找到一种方法来删除父元素。 I have a directive able to remove the element itself but not with the parent. 我有一个指令能够删除元素本身但不能删除父元素。 I tried adding parent.remove() instead of just remove() but I kept on getting error with AngularJS doesn't know what parent is. 我尝试添加parent.remove()而不是只remove()但我继续得到错误AngularJS不知道父是什么。

I have my html as 我有我的HTML

<ul ng-repeat="item in items | filter:{ pos: 'column1' }">
   <li>{{item.name}}<button remove-on-click ng-click="remove()" 
       class="remove-button fa fa-times"></button>
   </li>
</ul>

I have my directive as 我有我的指示

.directive('removeOnClick', function() {
  return {
     link: function (scope, element, attrs) {
       scope.remove = function () {
           element.remove();
       };
     }
   }
 });

Can someone please give me a hand? 有人可以帮我一把吗? Thanks in advance. 提前致谢。

omg! 我的天啊! I am so sorry everyone, I kept on trying parent and other ways and now I found out my stupidest mistake! 我很抱歉每个人,我一直在尝试parent和其他方式,现在我发现了我最愚蠢的错误! Just using parent().remove() would eventually work! 只使用parent().remove()最终会起作用!

Write $event inside your ng-click="remove()" function ,so the ng-click function will be ng-click="remove($event);" ng-click =“remove()”函数中写入$ event ,因此ng-click函数将是ng-click =“remove($ event);”

$scope.remove = function(e){
    console.log(e.target); // you can see button in console
    $(e.target).parent().remove();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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