我有一个嵌套的JSON结构如下:
[{
"phone_id" : "1",
"phone_name" : "nokia",
"phone_img" : "/src/imgs/nokia.jpg",
"phone_comments" :
[
{
"comment_id" : "1",
"user_id" : "32508",
"comment_date" : "2001-02-01",
"user_comment" : "This was the first phone that was rock solid from Nokia"
},
{
"comment_id" : "2",
"user_id" : "32518",
"comment_date" : "2001-02-02",
"user_comment" : "Great phone before the smartphone age"
},
{
"comment_id" : "3",
"user_id" : "22550",
"comment_date" : "2002-04-01",
"user_comment" : "Reminds me of my grandpa's phone"
},
{
"comment_id" : "4",
"user_id" : "31099",
"comment_date" : "2001-05-11",
"user_comment" : "It was a crappy one!"
}
]
}
]
显示部分(正常) - 我可以在第一个表格栏上显示手机图像,然后点击ng-click我在第二列中加载有关带有评论的手机信息。这完全没问题。
删除(不工作) - 我对删除评论有疑问。我不想删除整个手机对象,而只是删除特定的注释。我可以传递像???
这样的东西remove(comment, $index)
然后有一个执行以下操作的功能?
$scope.remove = function (index, comments) {
alert(comments.user_comment + index);
$scope.comments.splice(index, 1);
}
作为参考,HTML看起来像:
<div ng-app="angularJSApp">
<div ng-controller="PhoneCtrl">
<br/><br/><br/>
<table width="100%" border="1">
<tr ng-repeat="ph in phones">
<td width="20%"><a href="#" ng-click="showComments = ! showComments"><img width="50%" ng-src="{{ph.phone_img}}"></a></td>
<td>
<p>Phone Id: {{ph.phone_id}}</p>
<p>Phone Name: {{ph.phone_name}}</p>
<p>Number of comments: {{ph.phone_comments.length}}</p>
<div class="shComments" ng-show="showComments">
<p>Search: <input ng-model="query"></p>
<table border="1" width="100%">
<thead>
<tr>
<th><a href="" ng-click="predicate = 'comment_id'; reverse = !reverse">Id</a></th>
<th><a href="" ng-click="predicate = 'user_comment'; reverse = false">Comment</a>
(<a href="" ng-click="predicate = '-user_comment'; reverse = false">^</a>)
</th>
<th><a href="" ng-click="predicate = 'comment_date'; reverse = !reverse">Date</a></th>
<th><a href="" ng-click="predicate = 'user_id'; reverse = !reverse">User</th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="comment in ph.phone_comments | filter:query | orderBy:predicate:reverse">
<th>{{comment.comment_id}}
<th>{{comment.user_comment}}</th>
<th>{{comment.comment_date}}</th>
<th>{{comment.user_id}}</th>
<th><button ng-click="remove($index, comment)">Remove Comment</button>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</table>
</div>
</div>
P.S:我一直在试验AngularJS,我在尽可能多地寻找解决方案之后就问这个问题。谢谢你的帮助。
答案 0 :(得分:9)
您可以执行以下操作:
$scope.remove = function (index, comments) {
delete $scope.comments[index]
}
仔细观察后,您似乎有一个嵌套的数据结构,这意味着您需要两个索引:一个用于电话,另一个用于电话数据结构中的注释。
所以你需要的是一种方法:
$scope.remove = function (pIndex, cIndex) {
delete $scope.phones[pIndex].phone_comments[cIndex];
}
我提出的另一个建议是,你应该把手机变成一流的公民模式,并通过服务来操纵它们。
答案 1 :(得分:2)
感谢你们两位。第一个建议确实奏效了。
$scope.removeComment = function (pid, cid) {
$scope.phones[pid].phone_comments.splice(cid, 1);
};
来自HTML的调用是
<th><button ng-click="removeComment($parent.$index, $index)">Remove Comment</button>
答案 2 :(得分:1)
问题我发现您致电ng-click="remove($index, comment)
并传递了2个参数:$index
并选择了comment
。
但是,remove
方法适用于index
和列表的评论
$scope.remove = function (index, comments) {
alert(comments.user_comment + index);
$scope.comments.splice(index, 1);
}
将ng-click
更改为:
ng-click="remove($index, ph.phone_comments)
没有$ index的第二种方式:
ng-click="remove(comment, ph.phone_comments)
JS
$scope.remove = function(comment, comments) {
comments.splice(comments.indexOf(comment), 1);
};
<强> [编辑] * 强>
参见工作演示 Plunker