gpt4 book ai didi

javascript - AngularJS 1.x 如何使用资源获取结果数组?

转载 作者:行者123 更新时间:2023-11-30 16:32:00 25 4
gpt4 key购买 nike

这是我在 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。

我哪里出错了?

最佳答案

我认为您不需要在 $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= 的 URL 2 像这样。

服务

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

Controller

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

关于javascript - AngularJS 1.x 如何使用资源获取结果数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33196200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com