[英]Passing params to an angular service from a controller?
I'm an angular newby. 我是个有角的新人。 I'm hoping to pass params to a service that fetches data form a server depending on those params.
我希望将参数传递给根据这些参数从服务器获取数据的服务。 for example, if I want to pass a book name string and then use it in the service to concatenate with the request url.
例如,如果我要传递书名字符串,然后在服务中使用它来与请求url串联。 The documentation does not show clearly in this subject and I could not find helpful examples in other resources.
该文档在该主题中没有明确显示,在其他资源中也找不到有用的示例。
Let say, this is the controller: 假设这是控制器:
app.controller('BookController', ['$scope', '$routeParams', 'books', function($scope, $routeParams, books) {
// sending params to books service before a successful return
books.success(function(data) {
$scope.book = data[$routeParams.bookId];
});
and this is the service 这就是服务
app.factory('books', ['$http', function($http) {
// var url = 'http://...' + ParamFromController + '.json'
return $http.get(url)
.success(function(data) {
return data;
})
.error(function(err) {
return err;
});
}]);
So, how can I send params to 'books' service and then use it in the service? 那么,如何将参数发送到“书籍”服务,然后在服务中使用它? Thanks a lot in advance.
非常感谢。
You can declare your service as: 您可以将服务声明为:
app.factory('books', ['$http', function($http) {
// var url = 'http://...' + ParamFromController + '.json'
return {
getVal: function(url,options){
return $http.get(url,options)
}
}
}]);
and use it in your controller and provide appropriate params to pass into 'books' service: 并在控制器中使用它,并提供适当的参数以传递到“图书”服务中:
app.controller('BookController', ['$scope', '$routeParams', 'books', function($scope, $routeParams, books) {
// sending params to books service before a successful return
books.getVal('api/activity.json',{'name':'abc'}).success(function(data) {
$scope.book = data[$routeParams.bookId];
});
Also, dont use the .success()
callback both in your service
and controller function
. 另外,不要在
service
和controller function
都使用.success()
回调。 The books
service is returning a promise( $http
returns a promise implicitly) and you can handle that in controller. books
服务正在返回一个承诺( $http
隐式返回一个承诺),您可以在控制器中处理它。
Right now you are returning the promise / result of the $http
as the service instance. 现在,您将返回
$http
的Promise /结果作为服务实例。 Services are not meant to work this way. 服务并不意味着这种方式。 You should return an object that holds several properties / methods that define your service:
您应该返回一个对象,其中包含几个定义服务的属性/方法:
app.factory('books', ['$http', function($http) {
var instance = {
getBook: function(bookId) {
return $http.get(...);
}
}
return instance;
}
In the controller you can then use the books service as follows: 在控制器中,您可以按以下方式使用图书服务:
books
.getBook($routeParams.bookId)
.then(function (result) { ... });
app.factory('books', ['$http', function($http) {
var booksService = {};
booksService.getBook = function(bookId){
{
return $http.get(url, bookId);
};
return booksService;
}]);
and in your controller 并在您的控制器中
app.controller('BookController', ['$scope', '$routeParams', 'books', function($scope, $routeParams, books) {
books.getBook($routeParams.bookId).success(function(data) {
$scope.book = data;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.