你好我正在开发一个基于AngularJS和Laravel的应用程序,但是我对AngularJS部分有问题。
我有这段代码
$scope.archive = function() {
var oldnodes = $scope.nodes;
angular.forEach(oldnodes, function(node) {
if (node.done)
alert(node.id);
$http.get('http://localhost/copiaAngus/public/deleteSelected/(node.id)').success(function(data)
{
alert(node.id);/**Show a id of checkbox selected***/
$timeout(function() {
$location.path('/');
});
});
});
};
在警告消息中,您可以显示所选节点的ID,我不知道如何将所有id传递给laravel。
Laravel部分
Route::get("deleteSelected/{id}", function()
{
$posts = Nodes::destroy($id);
return Response::json(array(
"posts" => $posts
));
});
破坏正在使用此表单
$posts = Nodes::destroy(1,2);
答案 0 :(得分:0)
将一组ID传输到服务器有不同的方法。让我们将它们添加到URL并用分号分隔列表。所以我们的最终URL看起来像这样:
http://localhost/copiaAngus/public/deleteSelected/1;4;5
首先,我们需要更改服务器端代码才能接受此参数
Route::get("deleteSelected/{ids}", function($ids)
{
$ids_array = explode(';', $ids);
$posts = Nodes::destroy($ids_array);
return Response::json(array(
"posts" => $posts
));
});
现在,以下是如何生成网址
$scope.archive = function() {
// filter the nodes so we only have the "done" ones
var nodes = $scope.nodes.filter(function(node){
return node.done;
});
// now build a new array that only contains the ids
var ids = nodes.map(function(node){
return node.id;
});
$http.get('http://localhost/copiaAngus/public/deleteSelected/'+ids.join(';')).success(function(data){
alert(data.posts + ' post(s) deleted');
$timeout(function(){
$location.path('/');
});
});
};