为什么我无法从数组中删除项目?
删除项目的html标签:
删除项目的html标签:
html tag
<span ng-click="remove($index)"> delete</span>
//AngularJS method where I try to delete item
blog.remove = function(index) {
blog.posts.splice(index, 1);
};
//Angular array posts
blog.posts = [{
"title": "Blog Post One",
"comments": [
{
"body":"Lorem ipsum dolor sit amet, consectetur adipisicing elit. ",
"author": "trollguy87"
}
]}];
问题可能出在哪里?
remove
在作用域上被调用了。但是你的定义是在博客本身的。所以,它不应该是blog.remove($index)
吗?那么,$index
是什么?我猜是从某种repeat
中传来的。 - Chanthung-repeat
部分的代码,目前很难弄清楚您如何管理$scope/vm
。 - Icycoolvar blog = $scope;
并通过ng-click="remove()"
调用$scope.remove
时,这段代码才是正确的。 - vp_arth