我有一个 li
元素,根据 $scope.items
列表进行重复。每个 li
中都有一个复选框。我想要做的是捕获这个复选框的变化事件并在那里执行我的操作。在 $scope.change
函数中执行。
当我的工作完成后,我想要删除已选择的复选框所在的行。
目前我的代码如下:
<!doctype html>
<html>
<head>
<script src="http://code.angularjs.org/1.2.3/angular.min.js"></script>
<script>
var app = angular.module('myapp', []);
app.controller('mainController', function($scope) {
$scope.items = ["item1", "item2", "item3"];
$scope.change = function() {
// My work here.
// ...
// Work is done. remove the caller checkbox.
this.parent.remove(); // <--- BOOM.
}
});
</script>
</head>
<body ng-app="myapp">
<div ng-controller="mainController">
<ul>
<li ng-repeat="item in items">
<input type="checkbox" ng-model="checked" ng-change="change()">
</li>
</ul>
</div>
</body>
</html>
Live 版本在这里:http://plnkr.co/edit/05IBKp6aVoj1SqFNi5JJ
我的问题出现在这行代码中:
this.parent.remove(); // <--- BOOM.
我的目标是删除父级li
元素。
问题:
- 如何正确实现?
- 当我在控制器的change函数中使用
this
关键字时,是否可以使用JQuery语法来操作它?类似于$(this).parent().remove();
这样的语法?