[Memorandum] provider, factory, service, Hello world example

Source: Internet
Author: User


var myApp = angular.module(‘myApp‘, []);//service style, probably the simplest onemyApp.service(‘helloWorldFromService‘, function() { this.sayHello = function() { return "Hello, World!" };});//factory style, more involved but more sophisticatedmyApp.factory(‘helloWorldFromFactory‘, function() { return { sayHello: function() { return "Hello, World!" } };});//provider style, full blown, configurable version myApp.provider(‘helloWorld‘, function() { // In the provider function, you cannot inject any // service or factory. This can only be done at the // "$get" method. this.name = ‘Default‘; this.$get = function() { var name = this.name; return { sayHello: function() { return "Hello, " + name + "!" } } }; this.setName = function(name) { this.name = name; };});//hey, we can configure a provider! myApp.config(function(helloWorldProvider){ helloWorldProvider.setName(‘World‘);});function MyCtrl($scope, helloWorld, helloWorldFromFactory, helloWorldFromService) { $scope.hellos = [ helloWorld.sayHello(), helloWorldFromFactory.sayHello(), helloWorldFromService.sayHello()];}?

The value, factory, service, constant, and provider methods are all providers. They teach the injector how to instantiate the services.

1. The value recipe is the simplest case, where you instantiate the service yourself and provide theinstantiated value to the injector.

 

2. the factory recipe gives the injector a factory function that it callwhen it needs to instantiate the service. when called, the factory function creates and returns the service instance. the dependencies of the service are injected as the functions's arguments. so using this recipe adds the following abilities: the service recipe is almost the same as the factory recipe, but here the injector invokes aconstructor with the new operator instead of a factory function.

Ability to use other services (have dependencies)

Service Initialization

Delayed/lazy Initialization

 

3. The provider recipe is usually overkill. It adds one more layer of indirection by allowing you to configure the creation of the factory.

You shoshould use the provider recipe only when you want to expose an API for application-wide configuration that must be made before the application starts. this is usually interesting only for reusable services whose behavior might need to vary slightly between applications.


4. the constant recipe is just like the value recipe doesn't it allows you to define services that are available in the config phase. sooner than services created using the value recipe. unlike values, they cannot be decorated using decorator.

 relative information on stackoverflow:  http://stackoverflow.com/questions/15666048/angular-js-service-vs-provider-vs-factory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.