我正在尝试将变量放入Angularjs的get请求中。
这是一个有效的代码:
app.controller("CustomerDetailController", [
"$scope","$routeParams","$resource",
function($scope , $routeParams , $resource) {
var Customer = $resource('/customers/400.json');
$scope.customer = Customer.get();
}
]);
正如您所看到的,数字400在$ resource中的url中是硬编码的。现在,我想用变量替换它。我试图按照angular $resource上的文档,但我在某个地方犯了错误。这是我试过的:
app.controller("CustomerDetailController", [
"$scope","$routeParams","$resource",
function($scope , $routeParams , $resource) {
var Customer = $resource('/customers/:customerId.json', {customerId: '@id'});
$scope.customer = Customer.get({customerId: 400});
}
]);
有人能发现错误吗?
编辑:我添加了一些警告,以检查错误发生的位置:
alert("1. Ajax Call Initiated!"); //gets called
var customerId = 400;
alert("2. Ajax Call Initiated!"); //gets called
var Customer = $resource('/customers/:customerId.json', {customerId: '@id'});
$scope.customer = Customer.get({customerId: 400});
alert("3. Ajax Call Initiated!"); // This alert is NOT called
编辑2:
控制台会输出以下错误:
错误:encodeUriSegment不是函数...
编辑3:结果,我的角度组件没有相同的版本。
答案 0 :(得分:1)
编辑:问题是角度模块不一致(1.5.9对1.6)
app.controller("CustomerDetailController", [
"$scope","$routeParams","$resource",
function($scope , $routeParams , $resource) {
var Customer = $resource('/customers/:customerId.json', {customerId: '@id'});
Customer.get({customerId: 400}, function(customer, responseHeader){
$scope.customer = customer;
});
}
]);