我的列表项旁边有一个按钮,用于删除列表项。
我的按钮在列表中,所以我试图找到一种方法来删除父元素。我有一个指令能够删除元素本身但不能删除父元素。我尝试添加parent.remove()
而不仅仅是remove()
,但我一直在收到错误,AngularJS并不知道父母是什么。
我的 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>
我的指令为
.directive('removeOnClick', function() {
return {
link: function (scope, element, attrs) {
scope.remove = function () {
element.remove();
};
}
}
});
有人可以帮我一把吗? 提前谢谢。
答案 0 :(得分:3)
OMG!我很抱歉所有人,我继续尝试parent
和其他方式,现在我发现了我最愚蠢的错误!只使用parent().remove()
最终会有效!
答案 1 :(得分:0)
在 ng-click =“remove()”功能中写入 $ event ,因此ng-click功能将 ng-click =“删除($ event);“
$scope.remove = function(e){
console.log(e.target); // you can see button in console
$(e.target).parent().remove();
}