javascript AngularJS - 创建一个服务对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11251181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS - creating a service object
提问by bsr
Instead of posting in Angular mailing list, I think this may be more of javascript question. Hope the SO community can also give faster response.
我认为这可能更多是 javascript 问题,而不是在 Angular 邮件列表中发帖。希望SO社区也能给予更快的回应。
I am trying to encapsulate the data in a service and injecting into controller.
我试图将数据封装在服务中并注入控制器。
angular.module('myApp.services', ['ngResource']).
factory('Player', function($resource){
var Player ;
Player = {
resource: $resource('/api/Player/:_id', {} )
};
return Player
});
function PlayerDetailCtrl(Player, $routeParams, $scope) {
$scope.resource = Player.resource.get({_id:$routeParams._id});
}
PlayerDetailCtrl.$inject = ['Player', '$routeParams', '$scope'];
It throws an exception
它抛出异常
TypeError: Object #<Object> has no method 'query'
$scope.resource = Player.Player.resource.get({_id:$routeParams._id});
also throws error
$scope.resource = Player.Player.resource.get({_id:$routeParams._id});
也抛出错误
TypeError: Object #<Object> has no method 'query'
the below works.
下面的作品。
angular.module('myApp.services', ['ngResource']).
factory('Player', function($resource){
var Player ;
Player= $resource('/api/Player/:_id', {} )
return Player
});
function PlayerDetailCtrl(Player, $routeParams, $scope) {
$scope.resource = Player.Player.get({_id:$routeParams._id});
}
PlayerDetailCtrl.$inject = ['Player', '$routeParams', '$scope'];
my intention is to add more data and method to Player
. So how can I make the first (object form) works!
我的目的是将更多数据和方法添加到Player
. 那么我怎样才能使第一个(对象形式)起作用!
采纳答案by Dan Doyon
You are creating a factory, this is the atypical way of doing. You don't want to be returning an instance. Angular will give you an instance in your controller.
您正在创建一个工厂,这是非典型的做法。您不想返回一个实例。Angular 会在你的控制器中为你提供一个实例。
factory('Player', function ($resource) {
return $resource('/api/Player/:_id', { });
})
Here is a service I wrote to interact with a cakephp REST service. I wrote it a while back so just take it as an illustration, I may refactor.
这是我编写的与 cakephp REST 服务交互的服务。前段时间写的,就拿来举例说明吧,我可能会重构。
factory('CommentSvc', function ($resource) {
return $resource('/cakephp/demo_comments/:action/:id/:page/:limit:format', { id:'@id', 'page' : '@page', 'limit': '@limit' }, {
'initialize' : { method: 'GET', params: { action : 'initialize', format: '.json' }, isArray : true },
'save': { method: 'POST', params: { action: 'create', format: '.json' } },
'query' : { method: 'GET', params: { action : 'read', format: '.json' } , isArray : true },
'update': { method: 'PUT', params: { action: 'update', format: '.json' } },
});
}).
})。
回答by Grace Shao
Looks like the JS is complaining about some code for Player. I guess Player= $resource('/api/Player/:_id', {} )
makes Player
have a property call query
. The form you changed to does not make Player
have that property.
看起来 JS 正在抱怨 Player 的一些代码。我猜Player= $resource('/api/Player/:_id', {} )
使Player
有一个属性调用query
。您更改为的表单不Player
具有该属性。
回答by bsr
Ok, after few tries the following worked.
好的,经过几次尝试后,以下工作正常。
Player = {
resource: function() {
return $resource('/api/Player/:_id', {} )
}
};
In this I explicitly return resource object. But cannot explain how it differ from
在此我明确返回资源对象。但无法解释它与
resource: $resource('/api/Player/:_id', {} )
resource: $resource('/api/Player/:_id', {} )