2013年1月18日

AngularJS service, factory, provider 的差別


$provide.provider

$provide.provider is the most spohisticated method of registering blueprints, it allows you to have a complex creation function and configuration options.

You will be provided with new ProviderFunction().$get(). The constructor function is instantiated before the $get method is called.


$provide.factory

$provide.factory is a simplified version of $provide.provider when you don't need to support configuration options but still want to have a more sophisticated creation logic.

You will be provided the value that is returned by invoking the function reference passed to module.factory


$provide.service

$provide.service is for cases where the whole creation logic boils down to invoking a constructor function.

You will get an instance of the function. In other words new FunctionYouPassedToService().



範例:
http://jsbin.com/ohamub/1/edit