Skip to main content
Typo fixed.
Source Link
xis
  • 24.9k
  • 10
  • 47
  • 61

This answer address the topic/question

how Factory, Service and Constant — are just syntactic sugar on top of a provider recipe?

OR

how factory ,servic and providers are simailar interanllyinternally

basically what happens is

When you make a factory() it sets you function provided in second argument to provider's $get and return it(provider(name, {$get:factoryFn })), all you get is provider but there is no property/method other than $get of that provider(means you can't configure this)

Source code of factory

function factory(name, factoryFn, enforce) { return provider(name, { $get: enforce !== false ? enforceReturnValue(name, factoryFn) : factoryFn }); }; 

When making a service() it return you providing a factory() with a function that injects the constructor (return the instance of the constructor you provided in your service) and returns it

Source code of service

function service(name, constructor) { return factory(name, ['$injector', function($injector) { return $injector.instantiate(constructor); }]); }; 

So basically in both cases you eventually get a providers $get set to your function you provided , but you can give anything extra than $get as you can originally provide in provider() for config block

This answer address the topic/question

how Factory, Service and Constant — are just syntactic sugar on top of a provider recipe?

OR

how factory ,servic and providers are simailar interanlly

basically what happens is

When you make a factory() it sets you function provided in second argument to provider's $get and return it(provider(name, {$get:factoryFn })), all you get is provider but there is no property/method other than $get of that provider(means you can't configure this)

Source code of factory

function factory(name, factoryFn, enforce) { return provider(name, { $get: enforce !== false ? enforceReturnValue(name, factoryFn) : factoryFn }); }; 

When making a service() it return you providing a factory() with a function that injects the constructor (return the instance of the constructor you provided in your service) and returns it

Source code of service

function service(name, constructor) { return factory(name, ['$injector', function($injector) { return $injector.instantiate(constructor); }]); }; 

So basically in both cases you eventually get a providers $get set to your function you provided , but you can give anything extra than $get as you can originally provide in provider() for config block

This answer address the topic/question

how Factory, Service and Constant — are just syntactic sugar on top of a provider recipe?

OR

how factory ,servic and providers are simailar internally

basically what happens is

When you make a factory() it sets you function provided in second argument to provider's $get and return it(provider(name, {$get:factoryFn })), all you get is provider but there is no property/method other than $get of that provider(means you can't configure this)

Source code of factory

function factory(name, factoryFn, enforce) { return provider(name, { $get: enforce !== false ? enforceReturnValue(name, factoryFn) : factoryFn }); }; 

When making a service() it return you providing a factory() with a function that injects the constructor (return the instance of the constructor you provided in your service) and returns it

Source code of service

function service(name, constructor) { return factory(name, ['$injector', function($injector) { return $injector.instantiate(constructor); }]); }; 

So basically in both cases you eventually get a providers $get set to your function you provided , but you can give anything extra than $get as you can originally provide in provider() for config block

edit
Source Link
Pankaj Parkar
  • 136.4k
  • 23
  • 241
  • 307

This answer address the topic/question

how Factory, Service and Constant — are just syntactic sugar on top of a provider recipe?

or

how Factory, Service and Constant — are just syntactic sugar on top of a provider recipe?

how factory ,servic and providers are simailar interanllyOR

how factory ,servic and providers are simailar interanlly

basically what happens is

whenWhen you make a factory() it sets you functionfunction provided in second argument to provider's $get and return it(provider(name, {$get:factoryFn })), all you get is provider but no property/method other then $getall you get is provider but there is no property/method other than $get of that providerprovider(means you cantcan't configure this)

source of factorySource code of factory

function factory(name, factoryFn, enforce) { return provider(name, { $get: enforce !== false ? enforceReturnValue(name, factoryFn) : factoryFn });  }; 

whenWhen making a service()service() it return you providing a factory() with a functionfunction that injects the constructor constructor (return the instance of the constructor you provided in your service) and returns it

source code of serviceSource code of service

function service(name, constructor) { return factory(name, ['$injector', function($injector) { return $injector.instantiate(constructor); }]);  }; 

So basically in both cases you eventually get a providers $get set to your function you provided , but you can give anything extra than $get as you can originally provide in provider() for config block

This answer address the topic/question

how Factory, Service and Constant — are just syntactic sugar on top of a provider recipe?

or

how factory ,servic and providers are simailar interanlly

basically what happens is

when you make a factory() it sets you function provided in second argument to provider's $get and return it(provider(name, {$get:factoryFn })), all you get is provider but no property/method other then $get of that provider(means you cant configure this)

source of factory

function factory(name, factoryFn, enforce) { return provider(name, { $get: enforce !== false ? enforceReturnValue(name, factoryFn) : factoryFn });  } 

when making a service() it return you providing a factory() with a function that injects the constructor (return the instance of the constructor you provided in your service) and returns it

source code of service

function service(name, constructor) { return factory(name, ['$injector', function($injector) { return $injector.instantiate(constructor); }]);  } 

So basically in both cases you eventually get a providers $get set to your function you provided , but you can give anything extra than $get as you can originally provide in provider() for config block

This answer address the topic/question

how Factory, Service and Constant — are just syntactic sugar on top of a provider recipe?

OR

how factory ,servic and providers are simailar interanlly

basically what happens is

When you make a factory() it sets you function provided in second argument to provider's $get and return it(provider(name, {$get:factoryFn })), all you get is provider but there is no property/method other than $get of that provider(means you can't configure this)

Source code of factory

function factory(name, factoryFn, enforce) { return provider(name, { $get: enforce !== false ? enforceReturnValue(name, factoryFn) : factoryFn }); }; 

When making a service() it return you providing a factory() with a function that injects the constructor (return the instance of the constructor you provided in your service) and returns it

Source code of service

function service(name, constructor) { return factory(name, ['$injector', function($injector) { return $injector.instantiate(constructor); }]); }; 

So basically in both cases you eventually get a providers $get set to your function you provided , but you can give anything extra than $get as you can originally provide in provider() for config block

added 74 characters in body
Source Link
A.B
  • 20.5k
  • 3
  • 43
  • 74

This answer address the topic/question

how Factory, Service and Constant — are just syntactic sugar on top of a provider recipe?

or

how factory ,servic and providers are simailar interanlly

basically what happens is

when you make a factory()factory() it sets you function provided in second argument to provider's $get $get and return it(provider(name, {$get:factoryFn })), all you get is provider but nothing before the $getall you get is provider but no property/method other then $get of that provider(means you cant configure this)

source of factory

function factory(name, factoryFn, enforce) { return provider(name, { $get: enforce !== false ? enforceReturnValue(name, factoryFn) : factoryFn }); } 

when making a service() it return you providing a factory() with a function that injects the constructor (return the instance of the constructor you provided in your service) and returns it

source code of service

function service(name, constructor) { return factory(name, ['$injector', function($injector) { return $injector.instantiate(constructor); }]); } 

So basically in both cases you eventually get a providers $get set to your function you provided , but you can give anything extra than $get as you can originally provide in provider() for config block

This answer address the topic/question

how Factory, Service and Constant — are just syntactic sugar on top of a provider recipe?

or

how factory ,servic and providers are simailar interanlly

basically what happens is

when you make a factory() it sets you function provided in second argument to provider's $get , all you get is provider but nothing before the $get of that provider(means you cant configure)

source of factory

function factory(name, factoryFn, enforce) { return provider(name, { $get: enforce !== false ? enforceReturnValue(name, factoryFn) : factoryFn }); } 

when making a service() it return you providing a factory() with a function that injects the constructor (return the instance of the constructor you provided in your service) and returns it

source code of service

function service(name, constructor) { return factory(name, ['$injector', function($injector) { return $injector.instantiate(constructor); }]); } 

So basically in both cases you eventually get a providers $get set to your function you provided , but you can give anything extra than $get as you can originally provide in provider() for config block

This answer address the topic/question

how Factory, Service and Constant — are just syntactic sugar on top of a provider recipe?

or

how factory ,servic and providers are simailar interanlly

basically what happens is

when you make a factory() it sets you function provided in second argument to provider's $get and return it(provider(name, {$get:factoryFn })), all you get is provider but no property/method other then $get of that provider(means you cant configure this)

source of factory

function factory(name, factoryFn, enforce) { return provider(name, { $get: enforce !== false ? enforceReturnValue(name, factoryFn) : factoryFn }); } 

when making a service() it return you providing a factory() with a function that injects the constructor (return the instance of the constructor you provided in your service) and returns it

source code of service

function service(name, constructor) { return factory(name, ['$injector', function($injector) { return $injector.instantiate(constructor); }]); } 

So basically in both cases you eventually get a providers $get set to your function you provided , but you can give anything extra than $get as you can originally provide in provider() for config block

Source Link
A.B
  • 20.5k
  • 3
  • 43
  • 74
Loading