如何在angularjs中从局部视图的嵌套数组中删除项?
我在angular中使用了递归部分来显示注释线程。我有编辑和删除控件。正在删除服务器上的工作,但无法更新客户端。 我的主页:如何在angularjs中从局部视图的嵌套数组中删除项?,angularjs,Angularjs,我在angular中使用了递归部分来显示注释线程。我有编辑和删除控件。正在删除服务器上的工作,但无法更新客户端。 我的主页: <div class="list-group-item" ng-repeat="ch in img.comments" ng-include="'views/nestedComment.html'"> </div> 当我不使用partials时,这对我很有用。但是现在我无法访问父数组。您正在使用1个参数调用函数:deletenested
<div class="list-group-item" ng-repeat="ch in img.comments" ng-include="'views/nestedComment.html'">
</div>
当我不使用partials时,这对我很有用。但是现在我无法访问父数组。您正在使用1个参数调用函数:
deletenested(ch)
但是在函数表达式上需要2个参数$scope.deletenested=function(parent,item)
您应该将http调用移动到服务中,并在控制器外部管理数据模型。这样,它们将在任何地方都可用,并消除此问题。
<div class="list-group-item-info">
<h4>{{ch.app_user.email}}:</h4>
{{ch.body}}
<div class="controls" ng-if="ch.app_user_id==user.getUser().id">
<a href="#" ng-click="edit(ch)"><%=t :edit %></a>
<a href="#" ng-click="deletenested(ch)"><%=t :destroy %></a>
<a href="#" ng-click="answer(ch)"><%=t :answer %></a>
<a href="#" ng-click="nested = ! nested"><%=t :shownested %></a>
<div class="list-group-item-danger" ng-repeat="ch in ch.children" ng-show="nested" ng-include="'views/nestedComment.html'">
</div>
</div>
</div>
$scope.deletenested=function(parent,item){
$scope.$evalAsync(function(){
parent.children.splice(parent.children.indexOf(item),1);
})
$http.delete('/comments/'+item.id)
.success(function(){
alert('OK')
parent.children.splice(parent.children.indexOf(item),1)
})
.error(function(response,status){
if (status=='401'){
alert('You have to autorize')
$window.location.href='http://localhost:3000/app_users/sign_in';
}
})
}