O'Reilly logo

Dependency Injection with AngularJS by Alex Knol

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Angular service to the rescue

To circumvent all these problems, Angular has a concept of services that we mentioned at the end of Chapter 1, Learning to Fly. Talking to the Parse API or service can be viewed as a service to the application. The service is responsible for initializing Parse and instantiating the Reading object that is ready for manipulation. The service can then be injected in the two controllers and the shared code is nicely centralized:

// app/services.js
angular.module('serviceModule', [function ($provide) {
    $provide.factory('parse', [function () {
        // inti Parse SDK
        Parse.initialize("wNpkWu0OBGAAajJlnnqYPW3wsOT3T43LMn0e3VFb","04wqQGq62frJjEWzDOhISMrmtWDRFPjGuCoD4zWi");
    }]);
}]);

To make our service completely separate from our ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required