Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phungnc/7820258 to your computer and use it in GitHub Desktop.
Save phungnc/7820258 to your computer and use it in GitHub Desktop.
the difference between service, factory, provider in angular.js
// 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