【问题标题】:AngularJS 1.x How do I use resource to fetch an array of results?AngularJS 1.x 如何使用资源来获取结果数组?
【发布时间】:2016-01-16 17:23:48
【问题描述】:

这是我在 factory.js 中写的:

app.factory('CustomerCompany', function($resource){
    return $resource('/customer_companies/:id.json', {id: "@_id"}, {
        'query':{method: 'GET', isArray:false},
        'getByCustomerAccount':{
            method: 'GET', 
            params: {customer_account_id: customer_account_id}, 
            isArray:true, 
            url:'/customer_companies/get_by_account/:customer_account_id.json'
        }
    });
});

因为我想获取属于特定 customer_account 的 customer_companies 列表,所以我需要提供 customer_account_id。

在我的 controllers.js 中,

app.controller('customerAccountEditController', function($scope, CustomerCompany) {
    $scope.data = {};
    var path        = $location.path();
    var pathSplit   = path.split('/');
    $scope.id       = pathSplit[pathSplit.length-1];
    var customer_account_id = $scope.id;

    $scope.list_of_companies = [];

    CustomerCompany.getByCustomerAccount({customer_account_id: customer_account_id}, function(data){
        console.log(data);
        //$scope.list_of_delegates.push(data);
    }); 
});

我得到一个未定义的 customer_account_id。

我哪里做错了?

【问题讨论】:

    标签: javascript angularjs rest angularjs-service angular-resource


    【解决方案1】:

    我认为你不需要在 $resource 中定义 params & 然后 URL 将变为 url:'/customer_companies/get_by_account/customer_account_id.json' & 在调用方法时你需要从 {customer_account_id: customer_account_id} 传递 customer_account_id 以便它会创建一个带有customer_account_id=2 类似这样的URL。

    服务

    'getByCustomerAccount':{
          method: 'GET', 
          //params: {customer_account_id: customer_account_id}, //removed it
          isArray:true, 
          url:'/customer_companies/get_by_account/:customer_account_id'
    }
    

    控制器

    CustomerCompany.getByCustomerAccount({customer_account_id: customer_account_id + '.json'}, function(data){
        console.log(data);
        //$scope.list_of_delegates.push(data);
    }); 
    

    【讨论】:

    • 请注意,无需按照您建议的方式传入 .json 。我认为您可以简单地将其留在 url 中
    • 随意编辑答案..我会接受该编辑..这也将帮助遇到此类问题的其他人..
    【解决方案2】:

    这对我有用。

    controllers.js

    CustomerCompany.getByCustomerAccount({customer_account_id: customer_account_id}, function(data){
            console.log(data);
            //$scope.list_of_delegates.push(data);
        }); 
    

    services.js

    app.factory('CustomerCompany', function($resource){
        return $resource('/customer_companies/:id.json', {id: "@_id"}, {
            'query':{method: 'GET', isArray:false},
            'getByCustomerAccount':{
                method: 'GET', 
                isArray:false, 
                url:'/customer_accounts/:customer_account_id/customer_companies.json'
            }
        });
    });
    

    我改变了什么:

    1. 在服务器端,我决定使用/customer_accounts/:customer_account_id/customer_companies 来渲染结果。
    2. 我意识到返回的数据总是对象形式,所以我将$resource中的isArray改为false
    3. 当我调用getByCustomerAccount 时,我会按照控制器中 Pankaj Parkar 建议的方式传递参数

    【讨论】:

      【解决方案3】:

      params: {customer_account_id: customer_account_id},

      您尝试在此处分配的 customer_account_id 未定义。

      试试params: {customer_account_id: '@customer_account_id'},

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-10
        • 2012-07-20
        • 2016-03-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多