【问题标题】:Send id by get option通过 get 选项发送 id
【发布时间】:2014-11-20 15:08:42
【问题描述】:

您好,我正在开发一个基于 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
        ));

    });

destroy 正在使用这个表单

    $posts = Nodes::destroy(1,2);

【问题讨论】:

  • $http.get('http://localhost/copiaAngus/public/deleteSelected/'+node.id)?
  • 好的!我尝试执行此选项但不起作用:(我更新了问题。
  • 所以你想一次性将所有被选中的节点(node.done == true)的id发送到服务器?

标签: php angularjs laravel frameworks


【解决方案1】:

有多种方法可以将一组 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
    ));
});

现在,这是生成 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('/');
        });
    });
};

【讨论】:

  • 谢谢!我了解 php laravel 的所有部分,在 angularjs 部分我不明白为什么是 nodes.map ?它工作正常的代码!
  • map 函数让您可以遍历一个数组并创建一个包含返回值的新数组。所以在一个完整节点对象的数组中,我们创建了一个只有 id 的数组。
猜你喜欢
  • 1970-01-01
  • 2014-02-08
  • 2017-11-30
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多