我让这个简单的工厂返回$ resource:
app.factory('OusService', function ($resource,BASE_API) {
var res;
res = $resource(BASE_API+'/ou/:ouId',null,{
update: {
method: 'PUT' // this method issues a PUT request
}
});
return res;
});
然后我在名为'navCtrl'的控制器中使用$ $资源:
app.controller('navCtrl', ["$scope","$localStorage","OusService", function ($scope,$localStorage,OusService) {
var navOrg = $localStorage.navOrg;
OusService.query({ouId: navOrg.ou_id}).$promise.then(function(data){
console.log(data);
}, function(error) {
console.log(error);
});
}]);
该代码完全崩溃,但如果我删除'{ouId:navOrg.ou_id}',代码将被执行但查询不包含必需参数。我试着没有承诺,有回调,没有回调也没有承诺。代码总是与参数崩溃。
我也尝试在工厂中定义默认参数。
错误:
TypeError: V is not a function
at http://10.0.0.145:8003/layout1/assets/js/app.min.js:34:14237
at forEach (http://10.0.0.145:8003/layout1/assets/js/app.min.js:10:14316)
at C.setUrlParams (http://10.0.0.145:8003/layout1/assets/js/app.min.js:34:14122)
at Function.l.(anonymous function) [as query] (http://10.0.0.145:8003/layout1/assets/js/app.min.js:34:12722)
at new <anonymous> (http://10.0.0.145:8003/layout1/assets/js/controllers/navCtrl.js:13:16)
at Object.invoke (http://10.0.0.145:8003/layout1/assets/js/app.min.js:11:16221)
at extend.instance (http://10.0.0.145:8003/layout1/assets/js/app.min.js:13:8182)
at nodeLinkFn (http://10.0.0.145:8003/layout1/assets/js/app.min.js:12:11139)
at compositeLinkFn (http://10.0.0.145:8003/layout1/assets/js/app.min.js:12:2680)
at http://10.0.0.145:8003/layout1/assets/js/app.min.js:12:1389 <nav data-ng-include=" 'assets/views/partials/nav.html' " class="ng-scope" data-ng-animate="1">
编辑:
我将angularjs从1.5.7更新到1.5.11并且问题已经解决。
谢谢
答案 0 :(得分:0)
像这样使用..你试过像:
app.factory('OusService', function ($resource,BASE_API) {
var res;
res = $resource(BASE_API+'/ou/:ouId',{'@ouId':ouId},{
query: {
method: 'GET',isArray:true // or false if return an object
}
});
return res;
});
然后根据需要使用它
app.controller('navCtrl', ["$scope","$localStorage","OusService", function ($scope,$localStorage,OusService) {
var navOrg = $localStorage.navOrg;
OusService.query({ouId: navOrg.ou_id}).$promise.then(function(data){
console.log(data);
}, function(error) {
console.log(error);
});
}]);
答案 1 :(得分:0)
我不相信$resource
query
方法接受参数。相反,它返回一组结果。如果您想传递id
,我建议您使用get
OusService.get({ouId: navOrg.ou_id}).$promise.then(function(data){
console.log(data);
}, function(error) {
console.log(error);
});
另外,请尝试将资源更新为以下内容:
app.factory('OusService', ['$resource', 'BASE_API',
function($resource, BASE_API) {
var res;
res = $resource(BASE_API + '/ou/:ouId', {ouId: @ouId}, {
update: {
method: 'PUT'
}
});
return res;
}
]);
关键更改为:{ouId: @ouId}
以及['$resource', 'BASE_API',