以下是设置示例:
var editor = angular.module("editor",['ngResource']);
editor.factory("Provider",function($resource){
return $resource('/api/test/1').get();
})
function ctrl($scope,Provider){
$scope.sections = Provider;
}
我的问题是在firebug中我发现有两个GET请求被发送:一个在/ api / test / 1,一个在/ api / test / 1 /。
任何可能导致此问题的想法?
答案 0 :(得分:1)
为了完成,我相信$http
会在您的文件夹结构中“短路”,只需抓住您指向它的资源并且可以在项目中访问,而$resource
是适合与外部源通信,通常是外部RESTful api。在我实现它的应用程序中,我经常定义一个变量,我的各种$resource
定义可以访问它,正如您在我定义的变量API
在这种情况下,您还会将1
作为某种身份传递。
var API = 'http://localhost\\:#####/api/';
editor.factory('Provider', function ($resource) {
return $resource(API + 'test/:id', { id: '@id' }, { });
});
然后实施
function ctrl($scope, Provider){
var myVarToPass = 1;
Provider.get({ id: myVarToPass }, function (result) {
//single call here as soon as controller loads
});
}
答案 1 :(得分:1)
我发现只需在/#
之后添加/1
即可解决问题。
变化:
return $resource('/api/test/1')....
为:
return $resource('/api/test/1/#')....