【发布时间】:2016-10-30 14:02:01
【问题描述】:
为什么我不能从数组帖子中删除项目?
删除项目的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"
}
]}];
哪里有问题?
【问题讨论】:
-
一种可能性是您在删除函数中传递的索引是错误的。尝试在该 splice 语句之前通过 console.log 查看索引。
-
可能会发布更多代码。仅通过查看这些片段很难准确判断出什么问题。我看到的问题是
remove在范围内被调用。但是您的定义在博客本身上。那么,不应该是blog.remove($index)吗?还有,$index是什么?来自某种repeat我假设 -
请发布包含
ng-repeat部分的代码,目前很难弄清楚您如何管理$scope/vm -
如果只有
var blog = $scope;,此代码可能是正确的,因为您通过ng-click="remove()"调用$scope.remove -
请分享您程序中 html 部分的更多代码