enhanced version of factory. When a configurable factory is required, use provider.A brief introduction to the process of AngularJS running the application, divided into two phases, the Config phase and the run phase. The config phase is the stage of setting any provider. It is also the stage of setting any instructions, controllers, filters and other things. In the run phase,
defined service by invoking the Get method of the servicevar greeting = $injector. Get (' greeting '); Greeting (' Ford prefect ');The syringe is also responsible for injecting the service into the function:var myFunction = function (greeting) { greeting (' Ford prefect ');}; $injector. Invoke (MyFunction);Configure the provider:The provider allows a large number of configurations. When you create a service from a shortcut provided by
[Custom service VS factory VS provider in AngularJS]-What are their differences ?, Angularjsfactory
Before introducing AngularJS custom services, let's take a look at AngularJS ~
Anyone who has learned HTML knows that HTML is a good declarative language for pseudo-static text display design. However, it seems weak to
reusable, globally generic code, you don't need it.6. In the definition of all special objects, only the controller definition is configured by the factory manifest element.
Attribute/manifest Element type
Factory
Service
Value
Constant
Provider
Whether there can be dependencies
Yes
Yes
No
No
Yes
Support for using type injection
No
Yes
Yes*
Objective
3 ways to create custom services.
Factory
Service
Provider
As you should know, Angularjs is the backstage staff in the work outside the invention, he mainly applied the background of long-existing layered thinking. So we have to understand the role of the next layer, if you are the front-end people do not understand what is layered, then you'd better ask
Angularjs framework for a period of time, feeling very useful. The Angularjs app can be understood as PHP's Class,controller is the controller, and the built-in services and custom services can be understood as models. Angularjs has many built-in services, such as: $scope, $rootScope, $http, $q, $resource, $routeProvider and so on, here's how to customize the ser
1. IntroductionThe ANGULARJS employs an MVC architecture that provides a mechanism for registering services and using services in a dependency injection manner. A service is an abstraction of a function (method) and data (constant), such as a system in which information about a user's information (avatar, nickname, signature, birthday, gender, etc.) is abstracted and focused on an object, then the object can be considered a service. Services are avail
Angularjs Factory vs Service vs ProviderSource: Open Source China CommunityAuthor:Oschina
When you Angular, it's natural to heap unnecessary logic in the controller and scope. Be sure to realize early that the controller layer should be thin, that is, most of the business logic and persistent data in the application should be in service. I see a couple of similar questions on Stack Overflow every day about how to save persistent data i
service, we can inject the provider into the. config () methodAngular.module (' myApp '). config (function(userprovider) { Userprovider.setbackendurl ( "Http://myApiBackend.com/api");})So that we can use the service in the app like any other way.Angular.module (' myApp '). Controller (function($scope, User) { = user.save;});
When to use the provider () method
We ne
Learn Angular JSFactoryCan be thought of as a factory method in design mode, that is, you provide a method that returns an instance of an object;For Angularjs, the factory is to define an object, add properties and methods to the object, and then return the object.For example:var app = Angular.module ("MyApp", []);App.factory ("MyFactory", function () {var result = {};result.greeting = "Hello from factory";return result;});Finally, the controller gets
controller are the content returned through the $get () function. The following code writes the $get method to ' this ' (which will eventually be returned by the function). Now, $get function returns all the methods and properties that we want to access in the controller. The following is a code example:
Now, the complete code for provider is as follows:Now, like our factory and service, Setartist, Getartist, and callitune
Http://www.oschina.net/translate/angularjs-factory-vs-service-vs-providerWhen you Angular, it's natural to heap unnecessary logic in the controller and scope. Be sure to realize early that the controller layer should be thin, that is, most of the business logic and persistent data in the application should be in service. I see a couple of similar questions on Stack Overflow every day about how to save persistent data in a controller. This is not what
Defining a service using the provider method in $provideAs already known, the module definition is angular.module (NAME,[REQUIRES],CONFIGFN); The CONFIGFN parameter is the configuration service. The process of NG for service involves its dependency injection mechanism. Angularjs is an automatic dependency injection mechanism with $provider objects. $provide.
by the angular framework:Attention:
The Batchlog service relies on built-in $timeout and $log services, and allows messages to be exported to console.log log files.
Routetemplatemonitor services rely on built-in $route services like our custom batchlog services.
Our two services use factory methods to identify and group comments to inject annotations to declare their dependencies. It is important to note that the order of the strings in the array is consistent with the list of
accessed in your controller are the content returned through the $get () function. The following code writes the $get method to ' this ' (which will eventually be returned by the function). Now, $get function returns all the methods and properties that we want to access in the controller. The following is a code example:
Now, the complete code for provider is as follows:Now, like our factory and service, Setartist, Getarti
) { $provide. Factory (function() { return{ " StarCraft "}}) }) App.controller (function($scope, game) { = Game.title});"Factory" is really just shorthand for the provider which sets up a more generic provider which returns things or objects That has the "get" functions:/** * Created by Answer1215 on 12/27/2014.*/varApp = Angular.module ("app", []); app. Config (function($provide
();
}
});
Can be written
as App.factory (' MyDate ', function () {return
new Date ();
});
Can be written
as App.service (' mydate ', Date);
Summarize
All vendors are instantiated only once, and they are all single cases.
In addition to constant, all suppliers can be decorated by adorners (decorator)
Value is a simple, but injected,
Service is an injection builder
Factory is a method that can be injected.
Decorator can modify or encapsulate other suppliers, excep
, facetory111,service111,provider111) {//Custom, service name$scope. facetorytest = facetory111.firstname+ "" +facetory111.lastname ();$scope. servicetest = service111.firstname+ "" +service111.lastname ();$scope. providertest = provider111.firstname+ "" +provider111.lastname;}]); www.111cn.net
Third, create the app to put the service and controller together. App.js
The code is as follows
Copy Code
' Use strict ';/* APP Module */var Phonecatapp = angular.mo
The service itself is an arbitrary object, and the service process that NG provides involves its dependency injection mechanism.DOCTYPE HTML>HTMLLang= "en">Head> MetaCharSet= "UTF-8"> title>title>Head>Body> DivNg-app= "MYAPP"> DivNg-controller= "Firstcontroller">{{name}}Div> Div>Scripttype= "Text/javascript"src=".. /.. /vendor/angular/angularjs.js ">Script>Scripttype= "Text/javascript"src= "App/index.js">Script>Body>HTML>var myApp = angular.module (' myApp ', [],function ($pr
, opaquetoken// provide:any; useClass:TypeSummarizeAt the end of the article, I want to give a real-life example to help beginners better understand Angular 2 DI and Provider.Providertokencan be understood as the name of the dish, Useclass, usevalue can be understood as the cooking of the dish, and the dependent object is what we ordered, and DI system is our chef. If there is no chef, we have to care about what raw materials to cook this dish, how to cook, the important thing is to cook, can im
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.