博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【备忘录】provider, factory, service, hello world example
阅读量:4705 次
发布时间:2019-06-10

本文共 2646 字,大约阅读时间需要 8 分钟。

var myApp = angular.module('myApp', []); //service style, probably the simplest one myApp.service('helloWorldFromService', function() { this.sayHello = function() { return "Hello, World!" }; }); //factory style, more involved but more sophisticated myApp.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 calls when 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 should 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 except 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.

  

转载于:https://www.cnblogs.com/xiaoroad/p/3812091.html

你可能感兴趣的文章
如何使用Vue实现拖拽效果pageY、screenY、clientY、layerY、offsetY(转)
查看>>
[Bzoj1009][HNOI2008]GT考试(KMP)(矩乘优化DP)
查看>>
由于无法验证发布者 所以windows阻止此软件
查看>>
又是一道水的逆向思维题
查看>>
Linux内核分析— —操作系统是如何工作的(20135213林涵锦)
查看>>
圆角效果
查看>>
还原AdventureWorks2008示例数据库遇到的问题
查看>>
Java学习笔记--集合
查看>>
控件置顶[置顶] Android常用UI控件之ProgressBar
查看>>
HTML 知识点总结
查看>>
百度地图、高德地图的数据从哪里得到的?
查看>>
效率问题
查看>>
[Unity热更新]LuaFramework02.框架流程
查看>>
python3之paramiko模块
查看>>
Python Pandas -- DataFrame
查看>>
Windows安装TensorFlow
查看>>
创建一个JS函数,运用JS中arguments对象
查看>>
UML学习
查看>>
时间在数据库的保存方式
查看>>
自习任我行第二阶段个人总结6
查看>>