你好,我正在开发一个基于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。
拉拉维尔部分
Route::get("deleteSelected/{id}", function()
{
$posts = Nodes::destroy($id);
return Response::json(array(
"posts" => $posts
));
});
销毁正在使用此表单
$posts = Nodes::destroy(1,2);
有不同的方法可以将一组 id 传输到服务器。让我们将它们添加到 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
));
});
现在,这是生成URL的方法
$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('/');
});
});
};