<!DOCTYPE html> <html> <head> <script src="angular.js"></script> <script src="script.js"></script> </head> <body ng-app="mainModule"> <div ng-controller="mainController"> <strong>First name:</strong> {{personManagerInstance.getPersonFirstName()}}<br /> <strong>Last name:</strong> {{personManagerInstance.getPersonLastName()}}<br /> <strong>Full name:</strong> {{personManagerInstance.getPersonFullName("*")}}<br /> <br /> <label>Set the first name: <input type="text" ng-model="personInstance.firstName"/></label><br /> <label>Set the last name: <input type="text" ng-model="personInstance.lastName"/></label> </div> </body> </html>
// Definition of a PersonManager object var PersonManager = function () { return { $get: function (person) { return { getPersonFirstName: function () { return person.firstName; }, getPersonLastName: function () { return person.lastName; }, getPersonFullName: function (separator) { return person.firstName + separator + person.lastName; } }; } }; }; // Initialization of the "mainModule" angular.module("mainModule", []) // Register an object instance as a value and name it "person" .value("person", { firstName: "", lastName: "" }) // Register a provider with person management functions and name it "personManager". // This provider requires the "person" object instance registered as a value in the // "mainModule" and that instance is passed to the constructor through Dependency Injection // simply writing "person" (the name of the registered value) as parameter name. .provider("personManager", PersonManager) // Get the "person" registered object instance and the "personManager" service // instance (created by the "PersonManager" provider) through Dependency Injection. .controller("mainController", function ($scope, person, personManager) { // Initialize the values of the "person" instance registered // as an object in the "mainModule". This is the same instance // that the "personManager" service gets through Dependency Injection // because there's only a single instance registered with a given name. person.firstName = "John"; person.lastName = "Doe"; // Set variables on the scope to reference the "person" object instance // and the "personManager" service from the HTML template. $scope.personInstance = person; $scope.personManagerInstance = personManager; });



The most flexible way of registering an object creation recipe in an AngularJS module is given by the provider method.

A provider recipe is a function that returns an object containing a special function named $get. It's the $get function that returns the object instance and can have arguments useful to obtain other object instances through Dependency Injection (in the example, the $get function obtains the person value).

We said that a provider is the most flexible way of defining an object creation recipe because the $get function is inside an object that could contain other methods useful to configure the provider before an instance is actually created. You can learn more about this by taking a look at the example about the module's configuration and run phases.