8000 docs($provider): new example, more guidance on factory() vs service() by timruffles · Pull Request #4302 · angular/angular.js · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

docs($provider): new example, more guidance on factory() vs service() #4302

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
docs($provider): new example, more guidance on factory() vs service()
Currently $provide's example seems awkward. These examples are more real-world
- they use an injected service, and the service defined has a good reason to
be a singleton.
There's quite a lot of confusion around $provide, so I thought it'd
be good to make this page clearer.
Tests for example: http://jsbin.com/EMabAv/1/edit?js,output
Confusion on SO: http://stackoverflow.com/search?q=angularjs+service+vs+factory
  • Loading branch information
timruffles committed Oct 6, 2013
commit 4cc078a9154a3eb0a007c92442005c8fc7743954
90 changes: 64 additions & 26 deletions src/auto/injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,39 +276,76 @@ function annotate(fn) {
* a service. The Provider can have additional methods which would allow for configuration of the provider.
*
* <pre>
* function GreetProvider() {
* var salutation = 'Hello';
*
* this.salutation = function(text) {
* salutation = text;
* };
*
* this.$get = function() {
* return function (name) {
* return salutation + ' ' + name + '!';
* function TrackingProvider() {
* this.$get = function($http) {
* var observed = {};
* return {
* event: function(event) {
* var current = observed[event];
* return observed[event] = current ? current + 1 : 1;
* },
* save: function() {
* $http.post("/track",observed);
* }
* };
* };
* }
*
* describe('Greeter', function(){
*
* describe('Tracking', function() {
* var mocked;
* beforeEach(module(function($provide) {
* $provide.provider('greet', GreetProvider);
* $provide.provider('tracking', TrackingProvider);
* mocked = {post: jasmine.createSpy('postSpy')};
* $provide.value('$http',mocked);
* }));
*
* it('should greet', inject(function(greet) {
* expect(greet('angular')).toEqual('Hello angular!');
* it('allows events to be tracked', inject(function(tracking) {
* expect(tracking.event('login')).toEqual(1);
* expect(tracking.event('login')).toEqual(2);
* }));
*
* it('should allow configuration of salutation', function() {
* module(function(greetProvider) {
* greetProvider.salutation('Ahoj');
* });
* inject(function(greet) {
* expect(greet('angular')).toEqual('Ahoj angular!');
* });
* });
* it('posts to save', inject(function(tracking) {
* tracking.save();
* expect(mocked.post.callCount).toEqual(1);
* }));
* });
* </pre>
*
* There are also shorthand methods to define services that don't need to be configured beyond their `$get()` method.
*
* `service()` registers a constructor function which will be invoked with `new` to create the instance. You can specify services that will be provided by the injector.
*
* <pre>
* function TrackingProvider($http) {
* var observed = {};
* this.event = function(event) {
* var current = observed[event];
* return observed[event] = current ? current + 1 : 1;
* };
* this.save = function() {
* $http.post("/track",observed);
* };
* }
* $provider.service('tracking',TrackingProvider);
* </pre>
*
* `factory()` registers a function whose return value is the instance. Again, you can specify services that will be provided by the injector.
*
* <pre>
* function TrackingProvider($http) {
* var observed = {};
* return {
* event: function(event) {
* var current = observed[event];
* return observed[event] = current ? current + 1 : 1;
* },
* save: function() {
* $http.post("/track",observed);
* }
* };
* }
* $provider.factory('tracking',TrackingProvider);
* </pre>
*
*/

/**
Expand Down Expand Up @@ -336,7 +373,7 @@ function annotate(fn) {
* @methodOf AUTO.$provide
* @description
*
* A short hand for configuring services if only `$get` method is required.
* A service whose instance is the return value of `$getFn`. Short hand for configuring services if only `$get` method is required.
*
* @param {string} name The name of the instance.
* @param {function()} $getFn The $getFn for the instance creation. Internally this is a short hand for
Expand All @@ -351,7 +388,7 @@ function annotate(fn) {
* @methodOf AUTO.$provide
* @description
*
* A short hand for registering service of given class.
* A service whose instance is created by invoking `constructor` with `new`. A short hand for registering services which use a constructor.
*
* @param {string} name The name of the instance.
* @param {Function} constructor A class (constructor function) that will be instantiated.
Expand Down Expand Up @@ -619,3 +656,4 @@ function createInjector(modulesToLoad) {
};
}
}

0