I'm trying to convert an Angular HTTP.get
function in my controllers.js
to a service in services.js
.
The examples I have found all have conflicting ways to implement the service and their choice of names is confusing. Furthermore, the actual angular documentation for services uses yet a different syntax than all the examples. I know this is super simple, but please help me out here.
I have app.js
, controllers.js
, services.js
, filters.js
.
app.js
angular.module('MyApp', []). config(['$routeProvider', function($routeProvider) { $routeProvider. when('/bookslist', {templateUrl: 'partials/bookslist.html', controller: BooksListCtrl}). otherwise({redirectTo: '/bookslist'}); } ]);
controllers.js
function BooksListCtrl($scope,$http) { $http.get('books.php?action=query').success(function(data) { $scope.books = data; }); $scope.orderProp = 'author'; }
get() method is an asynchronous method that performs an HTTP get request in Angular applications and returns an Observable. And that Observable emits the requested data when the response is received from the server.
Use the HttpClient.get() method to fetch data from a server. The asynchronous method sends an HTTP request, and returns an Observable that emits the requested data when the response is received. The return type varies based on the observe and responseType values that you pass to the call.
Angular offers HttpClient to work on API and handle data easily. In this approach HttpClient along with subscribe() method will be used for fetching data.
Think in terms of modules and dependency injection.
So, lets say you have these three files
<script src="controllers.js"></script> <script src="services.js"></script> <script src="app.js"></script>
You would need three modules
angular.module('MyApp', ['controllers', 'services']) .config(['$routeProvider', function($routeProvider){ $routeProvider .when('/bookslist', { templateUrl: 'partials/bookslist.html', controller: "BooksListCtrl" }) .otherwise({redirectTo: '/bookslist'}); }]);
Notice that the other two modules are injected into the main module, so their components are available to the whole app.
Currently your controller is a global function, you might want to add it into a controllers module
angular.module('controllers',[]) .controller('BooksListCtrl', ['$scope', 'Books', function($scope, Books){ Books.get(function(data){ $scope.books = data; }); $scope.orderProp = 'author'; }]);
Books
is passed to the controller function and is made available by the services module which was injected in the main app module.
This is where you define your Books
service.
angular.module('services', []) .factory('Books', ['$http', function($http){ return{ get: function(callback){ $http.get('books.json').success(function(data) { // prepare data here callback(data); }); } }; }]);
There are multiple ways of registering services.
My preference in using the factory
function and just have it return an object. In the example above we just return an object with a get
function that is passed a success callback. You could change it to pass an error function too.
Edit Answering @yair's request in the comments, here's how you would inject a service into a directive.
I follow the usual pattern, first add the js file
<script src="directives.js"></script>
Then define a new module and register stuff, in this case a directive
angular.module('directives',[]) .directive('dir', ['Books', function(Books){ return{ restrict: 'E', template: "dir directive, service: {{name}}", link:function(scope, element, attrs){ scope.name = Books.name; } }; }]);
Inject the directive module to the main module in app.js
.
angular.module('MyApp', ['controllers', 'services', 'directives'])
You might want to follow a different strategy and inject a module into the directives module
Notice that the syntax inline dependency annotation is the same for almost everything. The directive is injected the same Books service.
Updated Plunker: http://plnkr.co/edit/mveUM6YJNKIQTbIpJHco?p=preview
Here is an implementation of service service instead of above mentioned factory service. Hope it helps.
app.js
angular.module('myApp', ['controllers', 'services']) .config(['$routeProvider', function($routeProvider){ $routeProvider .when('/bookslist', { templateUrl: 'partials/bookslist.html', controller: "BooksListCtrl" }) .otherwise({redirectTo: '/bookslist'}); }]);
service.js
angular.module('services', []) .service('books', ['$http', function($http){ this.getData = function(onSuccess,onError){ $http.get('books.json').then( onSuccess,onError); } }]);
controller.js
angular.module('controllers',[]) .controller('BooksListCtrl', ['$scope', 'books', function($scope, books){ $scope.submit = function(){ $scope.books = books.getData($scope.onSuccess,$scope.onError); } $scope.onSuccess = function(data){ console.log(data); $scope.bookName = data.data.bookname; } $scope.onError = function(error){ console.log(error); } }]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With