Services match providers

Services and Factory are important parts of Angular 1x where we communicate with a remote server for data. We used to define APIs call inside factory functions that controllers calls for fetching data from the servers:

 // Factory method in Angular 1x  angular.module('wedding.services', []) // DI of $http for HTTP calls to servers // $q  for promises .factory('CategoryService', function ($http, $q) {     var catalog   = {};     catalog.getCategories = function () {         // here we will call APIs     } })

Now you will see how we have migrated our code to Angular 4 providers. The same getCategories() method that was inside factory in Angular 1, will now be moved inside the CategoryData() class in Angular 4:

 // Provider in Angular ...

Get Hybrid Mobile Development with Ionic now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.