Created
December 6, 2013 08:15
-
-
Save phungnc/7820258 to your computer and use it in GitHub Desktop.
the difference between service, factory, provider in angular.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Below is the source code pick from angular.js | |
// Read source code help us more clearer | |
// the difference between service, factory, provider | |
function provider(name, provider_) { | |
if (isFunction(provider_) || isArray(provider_)) { | |
provider_ = providerInjector.instantiate(provider_); | |
} | |
if (!provider_.$get) { | |
throw Error('Provider ' + name + ' must define $get factory method.'); | |
} | |
return providerCache[name + providerSuffix] = provider_; | |
} | |
function factory(name, factoryFn) { return provider(name, { $get: factoryFn }); } | |
function service(name, constructor) { | |
return factory(name, ['$injector', function($injector) { | |
return $injector.instantiate(constructor); | |
}]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment