AngularJS service encapsulation reusable code
It is easy to create a service component in AngularJS. You only need to define a constructor with the $ get method, and then use the module's provider Method for registration: // define the constructor var myServiceProvider = function () {this. $ get = function () {return ....};}; // register angular in the module. module ("myModule", []). provider ("myService", myServiceProvider); The example defines a service that supports four arithmetic operations: ezCalculator. Configurable services sometimes require different behaviors of services in different scenarios, which means that services can be configured. For example, if we want the small calculator to append the currency symbol prefix to the computing result based on different localization regions, we need to configure the value of the localization region before the service is created, then, select the appropriate currency symbol based on the value in the specific calculation. AngularJS uses the config () method of the module to configure the service. It needs to inject the instantiated service provider (rather than the service instance) into the configuration function: angular. module ("myModule", []). config (["myServiceProvider", function (myServiceProvider) {// do some configuration.}]); note: The Registration Name of the provider object in the injector is service name + Provider. For example, the name of the $ http service provider instance is "$ httpProvider ". The service definition syntax sugar uses the module's provider method to define service components, which is somewhat cumbersome in some scenarios. AngularJS provides some simplified definition methods. These methods are generally only encapsulation of the provider method and apply to different application scenarios: factory uses an object factory function to define services, calling the factory function will return the service instance. A service uses a class constructor to define a service. The new operator is used to create a service instance. A value defines a service. The value is a service instance. Constant uses a constant to define a service. This constant is a service instance. The factory method of the factory method requires that an object factory be provided. The factory that calls this class will return the service instance. Var myServiceFactory = function () {return ...}; angular. module ("myModule", []). factory ("myService", myServiceFactory); INSIDE: AngularJS encapsulates the factory method as provider. The preceding example is equivalent to var myServiceFactory = function () {return ...}; angular. module ("myModule", []). provider ("myService", function () {this. $ get = myServiceFactory;}); (optional) The ezCalc that is rewritten using the factory method is preset. The ulator example shows the difference with the provider method! The service method requires a constructor. AngularJS uses this constructor to create a service instance: var myServiceClass = function () {this. method1 = function (){...}}; angular. module ("myModule", []). service ("myService", myServiceClass); INSIDE: AngularJS encapsulates the service method as provider. The preceding example is equivalent to var myServiceClass = function () {// class definition .}; angular. module ("myModule", []). provider ("myService", function () {this. $ get = function () {return new myServiceClass (); };}); (Optional) The ezCalculator example rewritten using the service method is preset. Feel the difference between the ezCalculator and the factory method! The value method sometimes needs to share a variable between different components, which can be considered as a service: the value of the variable is always returned by the provider. The value method provides a simplified encapsulation of this situation: angular. module ("myModule", []). value ("myValueService", "cx129800123"); INSIDE: AngularJS encapsulates the value method as provider. The preceding example is equivalent to angular. module ("myModule", []). provider ("myService", function () {this. $ get = function () {return "cx129800123" ;};}); the constant method sometimes needs to share a constant between different components, which can be considered as a service: the value of a constant returned by the provider. The constant method provides a simplified encapsulation for this situation: angular. module ("myModule", []). constant ("myConstantService", "Great Wall"); unlike the value method, AngularJS does not encapsulate the constant method into a provider, but registers the value internally. This allows constants to be used in the startup configuration phase of AngularJS (before any service is created): You can inject constants into the config () method of the module.