Adding Providers to AngularJS Modules

The Module object provides several helper methods for adding providers as an alternative to using the config() method. These methods are simpler to use and clearer in your code. You can add two types of provider objects to AngularJS modules. Each of these methods accepts two parameters: the name that will be registered with the dependency injector and the provider function that defines how to build the specific object. The following sections describe these methods in more detail.

Specialized AngularJS Object Providers

The Module object provides special constructor methods to add providers for the AngularJS objects that you need to implement in your modules. These specialized methods allow you to add definitions for the following types of objects:

Image animation(name, animationFactory)

Image controller(name, controllerFactory)

Image filter(name, filterFactory)

Image directive(name, directiveFactory)

The reason these are specialized methods is that there are corresponding animation, controller, filter, and directive objects defined in AngularJS for these provider methods.

Each of these objects is covered in more detail in later chapters. For now, here’s a quick look at a basic controller definition:

var mod = angular.module('myMod', []);
mod.controller('myController', function($scope) {
  $scope.someValue = 'Some Value';
});

A simple module named mod is created, and then the controller() method is called and passed in myController along with a controllerFactory function. The controllerFactory function accepts the $scope variable as a parameter. This is because AngularJS has a built-in controller object and knows that all controller objects must receive a scope object as the first parameter.

Service Providers

The service providers are a unique category of providers because there is not already a specific format for the resulting provider objects. Instead, a provider acts as a service to provide functionality. AngularJS provides some specific creation methods for building services and exposes them through the following methods:

Image value(name, object): This is the most basic of all providers. The object parameter is simply assigned to name, so there is a direct correlation in the injector between the name value and the object value.

Image constant(name, object): This is similar to the value() method, but the value is not changeable. Also, constant() methods are applied before other provider methods.

Image factory(name, factoryFunction): This method uses the factoryFunction parameter to build an object that will be provided by the injector.

Image service(name, serviceFactory): This method adds the concept of implementing a more object-oriented approach to the provider object. Much of the built-in functionality of AngularJS is provided through service providers.

Image provider(name, providerFactory): This method is the core for all the other methods. Although it provides the most functionality, it is not used frequently because the other methods are simpler.

Later chapters cover these objects in more detail. For now, here’s a quick example of some basic value and constant definitions:

var mod = angular.module('myMod', []);
mod.constant("cID", "ABC");
mod.value('counter', 0);
mod.value('image', {name:'box.jpg', height:12, width:20});

A simple module named mod is created, and then the constant() and two value() providers are defined. The values defined in these methods are registered in the injector server for the myMod module and are then accessible by name.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.134.188