Services (服务)
*/
.hljs {
display: block; padding: 0.5em;
background: #F0F0F0;
}
.hljs,
.hljs-subst,
.hljs-tag .hljs-title,
.lisp .hljs-title,
.clojure .hljs-built_in,
.nginx .hljs-title {
color: black;
}
.hljs-string,
.hljs-title,
.hljs-constant,
.hljs-parent,
.hljs-tag .hljs-value,
.hljs-rules .hljs-value,
.hljs-rules .hljs-value .hljs-number,
.hljs-preprocessor,
.hljs-pragma,
.haml .hljs-symbol,
.ruby .hljs-symbol,
.ruby .hljs-symbol .hljs-string,
.hljs-aggregate,
.hljs-template_tag,
.django .hljs-variable,
.smalltalk .hljs-class,
.hljs-addition,
.hljs-flow,
.hljs-stream,
.bash .hljs-variable,
.apache .hljs-tag,
.apache .hljs-cbracket,
.tex .hljs-command,
.tex .hljs-special,
.erlang_repl .hljs-function_or_atom,
.asciidoc .hljs-header,
.markdown .hljs-header,
.coffeescript .hljs-attribute {
color: #800;
}
.smartquote,
.hljs-comment,
.hljs-annotation,
.hljs-template_comment,
.diff .hljs-header,
.hljs-chunk,
.asciidoc .hljs-blockquote,
.markdown .hljs-blockquote {
color: #888;
}
.hljs-number,
.hljs-date,
.hljs-regexp,
.hljs-literal,
.hljs-hexcolor,
.smalltalk .hljs-symbol,
.smalltalk .hljs-char,
.go .hljs-constant,
.hljs-change,
.lasso .hljs-variable,
.makefile .hljs-variable,
.asciidoc .hljs-bullet,
.markdown .hljs-bullet,
.asciidoc .hljs-link_url,
.markdown .hljs-link_url {
color: #080;
}
.hljs-label,
.hljs-javadoc,
.ruby .hljs-string,
.hljs-decorator,
.hljs-filter .hljs-argument,
.hljs-localvars,
.hljs-array,
.hljs-attr_selector,
.hljs-important,
.hljs-pseudo,
.hljs-pi,
.haml .hljs-bullet,
.hljs-doctype,
.hljs-deletion,
.hljs-envvar,
.hljs-shebang,
.apache .hljs-sqbracket,
.nginx .hljs-built_in,
.tex .hljs-formula,
.erlang_repl .hljs-reserved,
.hljs-prompt,
.asciidoc .hljs-link_label,
.markdown .hljs-link_label,
.vhdl .hljs-attribute,
.clojure .hljs-attribute,
.asciidoc .hljs-attribute,
.lasso .hljs-attribute,
.coffeescript .hljs-property,
.hljs-phony {
color: #88F
}
.hljs-keyword,
.hljs-id,
.hljs-title,
.hljs-built_in,
.hljs-aggregate,
.css .hljs-tag,
.hljs-javadoctag,
.hljs-phpdoc,
.hljs-yardoctag,
.smalltalk .hljs-class,
.hljs-winutils,
.bash .hljs-variable,
.apache .hljs-tag,
.go .hljs-typename,
.tex .hljs-command,
.asciidoc .hljs-strong,
.markdown .hljs-strong,
.hljs-request,
.hljs-status {
font-weight: bold;
}
.asciidoc .hljs-emphasis,
.markdown .hljs-emphasis {
font-style: italic;
}
.nginx .hljs-built_in {
font-weight: normal;
}
.coffeescript .javascript,
.javascript .xml,
.lasso .markup,
.tex .hljs-formula,
.xml .javascript,
.xml .vbscript,
.xml .css,
.xml .hljs-cdata {
opacity: 0.5;
}
pre, code {
font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
}
pre ol.linenums {
margin-left: 0;
}
.pln {
color: #48484C; }
.str {
color: #DD1144; }
.kwd {
color: #1E347B; }
.com {
color: #93A1A1; }
.typ {
color: teal; }
.lit {
color: #195F91; }
.pun {
color: #93A1A1; }
.opn {
color: #93A1A1; }
.clo {
color: #93A1A1; }
.tag {
color: #008; }
.atn {
color: teal; }
.atv {
color: #DD1144; }
.dec {
color: teal; }
.var {
color: teal; }
.fun {
color: #DC322F; }
/* Put a border around prettyprinted code snippets. */
pre.prettyprint {
background-color: #F7F7F9;
padding: 10px;
border: 1px solid #E1E1E8; }
pre.prettyprint.linenums {
box-shadow: 40px 0 0 #FBFBFC inset, 41px 0 0 #ECECF0 inset; }
/* Specify class=linenums on a pre to get line numbering */
ol.linenums {
color: #1E347B;
margin: 0 0 0 40px; }
ol.linenums li {
color: #BEBEC5;
line-height: 18px;
padding-left: 12px; }
li.L0,
li.L1,
li.L2,
li.L3,
li.L5,
li.L6,
li.L7,
li.L8 {
list-style-type: normal; }
.wiz-todo,
.wiz-todo-img {
width: 16px;
height: 16px;
cursor: default;
padding: 0 10px 0 2px;
vertical-align: -10%;
-webkit-user-select: none;
}
.wiz-todo-label {
display: inline-block;
padding-top: 7px;
padding-bottom: 6px;
line-height: 1.5;
}
.wiz-todo-label-checked {
/*text-decoration: line-through;*/
color: #666;
}
.wiz-todo-label-unchecked {
text-decoration: initial;
}
.wiz-todo-completed-info {
padding-left: 44px;
display: inline-block;
}
.wiz-todo-avatar {
width: 20px;
height: 20px;
vertical-align: -20%;
margin-right: 10px;
border-radius: 2px;
}
.wiz-todo-account,
.wiz-todo-dt {
color: #666;
}
-->
Services(服务)
Angular services are substitutable objects that are wired together using dependency injection (DI). You can use services to organize and share code across your app.
Angular services are:
Lazily instantiated - Angular only instantiates a service when an application component depends on it.
Singletons - Each component dependent on a service gets a reference to the single instance generated by the service factory.
Angular offers several useful services (like $http), but for most applications you'll also want to create your own.
Angular服务是使用依赖注入串起来的可替换的对象。你可以用服务来为你的应用组织和共享代码。
Angular的服务有这些特性:
懒初始化:Angular只有在应用中某个组件依赖它的时候才初始化
单例:每个依赖某服务的组件只获取该服务的一个引用,这些引用都指向服务工厂创建的那个实例(单例)
Note: Like other core Angular identifiers, built-in services always start with $ (e.g. $http).
Using a Service(使用服务)
To use an Angular service, you add it as a dependency for the component (controller, service, filter or directive) that depends on the service. Angular's dependency injection takes care of the rest.
咋使用服务呢?给你的组件添加一个对它的依赖就可以了。剩下的工作交给Angular的依赖注入机制即可。
index.html:
<div id = "simple" ng-controller = "MyController" >
<p> Let's try this simple notify service, injected into the controller... </p>
<input ng-init = "message='test'" ng-model = "message" >
<button ng-click = "callNotify(message);" > NOTIFY </button>
<p> (you have to click 3 times to see an alert) </p>
</div>
script.js:
angular .
module ( 'myServiceModule' , []).
controller ( 'MyController' , [ '$scope' , 'notify' , function ( $scope , notify ) {
$scope . callNotify = function ( msg ) {
notify ( msg );
};
}]).
factory ( 'notify' , [ '$window' , function ( win ) {
var msgs = [];
return function ( msg ) {
msgs . push ( msg );
if ( msgs . length == 3 ) {
win . alert ( msgs . join ( "\n" ));
msgs = [];
}
};
}]);
Creating Services(创建服务)
Application developers are free to define their own services by registering the service's name and service factory function, with an Angular module.
The service factory function generates the single object or function that represents the service to the rest of the application. The object or function returned by the service is injected into any component (controller, service, filter or directive)
that specifies a dependency on the service.
开发者可以通过把服务的名字和工厂方法注册进一个Angular模块中,来定义自己的服务。
服务工厂方法是用来创建这个服务的单例对象或方法的。这个创建出来的服务(无论是个对象还是方法)就会被注入到依赖它的组件中去。
Registering Services(注册服务)
Services are registered to modules via the Module API. Typically you use the Module factory API to register a service:
var myModule = angular . module ( 'myModule' , []);
myModule . factory ( 'serviceId' , function () {
var shinyNewServiceInstance ;
// factory function body that constructs shinyNewServiceInstance
return shinyNewServiceInstance ;
});
Note that you are not registering a service instance, but rather a factory function that will create this instance when called.
可以通过Module API来注册服务。一般情况下,使用Module的factory API来注册。
注意啊,你注册的不是服务的实例,而是一个能创建服务实例的工厂函数。
Dependencies(依赖项)
Services can have their own dependencies. Just like declaring dependencies in a controller, you declare dependencies by specifying them in the service's factory function signature. The example module below has two services, each with various dependencies:
服务也可以有自己的依赖项。类似于控制器中声明依赖关系,我们在注册服务的时候,也可以使用类似的写法来为服务声明依赖项。
var batchModule = angular . module ( 'batchModule' , []);
/**
* The 'batchLog' service allows for messages to be queued in memory and flushed
* to the console.log every 50 seconds.
*
* @param {*} message Message to be logged.
*/
batchModule . factory ( 'batchLog' , [ '$internal' , '$log' , function ( $internal , $log ) {
var messageQueue = [];
function log () {
if ( messageQueue . length ) {
$log . log ( 'batchLog message: ' , messageQueue );
messageQueue = [];
}
}
// start periodic checking
$internal ( log , 50000 );
return function ( message ) {
messageQueue . push ( message );
}
}]);
/**
* 'routeTemplateMonitor' monitors each '$route' change and logs the current
* template via the 'batchLog' service.
*/
batchModule . factory ( 'routeTemplateMonitor' , [ '$route' , 'batchLog' , '$rootScope' ,
function ( $route , batchLog , $rootScope ) {
$rootScope . $on ( '$routeChangeSuccess' , function () {
batchLog ( $route . current ? $route . current . template : null );
});
}]);
In the example, note that:
The batchLog service depends on the built-in $internal and $log services.
The routeTemplateMonitor service depends on the built-in $route service and our custom batchLog service.
Both services use the array notation to declare their dependencies.
The order of identifiers in the array is the same as the order of argument names in the factory function.
上面的例子里,注意下:
batchLog服务依赖于内置服务$internal和$log
routeTemplateMonitor服务依赖于内置的$route服务,也依赖于我们的batchLog服务
这俩自定义服务都是使用array记法声明的依赖关系
依赖注入的顺序和工厂函数的参数顺序一定要对上
Registering a Service with $provide(用$provide来注册服务)
You can also register services via the $provide service inside of a module's config function:
angular . module ( 'myModule' , []). config ([ '$provide' , function ( $provide ) {
$provide . factory ( 'serviceId' , function () {
var shinyNewServiceInstance ;
// factory function body that constructs shinyNewServiceInstance
return shinyNewServiceInstance ;
});
}]);
This technique is often used in unit tests to mock out a service's dependencies.
当然,你也可以在模块的config方法里,使用$provide来注册服务。
不过这种技术通常用于单元测试中,模拟服务依赖项的时候用。
Services (服务)的更多相关文章
- android学习日记19--四大组件之Services(服务)
一个Android应用主要由四个基本组件组成,Android四大基本组件分别是Activity,Content Provider内容提供者,Service服务,BroadcastReceiver广播接 ...
- PHP 调用asp.net Web Services服务问题总结
原文:PHP 调用asp.net Web Services服务问题总结 PHP是弱类型语言,转换非常不方便. < ?php //soap 客户端 $client=new SoapClient(' ...
- Integration Services 服务连接失败,拒绝访问以及无法检索数据报错问题
第一个方法比较简单:把域账号添加admin组即可: 第二种方法: 添加域账号到分布式 COM 组 命令提示符下运行 dcomcnfg.exe 下一步 下一步 启动和激活权限 下一步 访问权限 同上设置 ...
- Power BI 与 Azure Analysis Services 的数据关联:1、建立 Azure Analysis Services服务
Power BI 与 Azure Analysis Services 的数据关联:1.建立 Azure Analysis Services服务
- 为何使用Microsoft SQL Server Management Studio连接Integration Services服务失败
检查是否满足以下各项: 1. 首先你要确保当前你使用的Windows账号是有管理员权限的 2. 其次请在打开Microsoft SQL Server Management Studio时,通过右键Ru ...
- HanLP封装为web services服务的过程介绍
前几天的召开的2019年大数据生态产业大会不知道大家关注到没有,看到消息是hanlp2.0版本发布了.不知道hanlp2.0版本又将带来哪些新的变化?准备近期看能够拿到一些hanlp2.0的资料,如果 ...
- Windows server 2008启动remote dosktop services服务报错1079
原创 欢迎转载,届时请注明出处 报错场景 今天,刚开始启动win server的远程连接服务(remote desktop services)时 该服务可以正常启动,然后选择了 “计算机--> ...
- 1.3 Services - 服务
服务是一种应用组件,它可以在后台执行耗时的操作,它是没有用户界面的.其它的应用组件都可以开启一个服务,服务开启后,即使用户离开了应用,服务仍然可以在后台运行.此外,绑定到服务的组件可以与服务进行交互, ...
- 基于soap 的 python web services 服务开发指南
文章大纲 序言 相关概念 SOA web services SOAP WSDL UDDI 环境搭建 我们使用 python 3.6 这个较新python 版本 服务端开发 客户端开发 suds-jur ...
随机推荐
- 一般处理程序中使用session
首先引用:using System.Web.SessionState; 再在 IHttpHandler 后面加逗号加IReadOnlySessionState:IHttpHandler,IReadO ...
- google map 计算地图面积方法
花了几个小时把js的google计算地图面积的算法改成了c# 的. class Program { static void Main(string[] args) { // a = new qq.ma ...
- 【补充版】HashMap(根据value筛选查找)
HashMap查找之根据Value查找 一般大家都知道对于HashMap而言都是通过key来进行查找.找到了key自然对应的value也就一并找到了.但是有些情况下就需要通过value来进行判断查找. ...
- ef 对象无法序列化的问题(System.Data.Entity.DynamicProxies)
错误提示: System.InvalidOperationException: 生成 XML 文档时出错. ---> System.InvalidOperationException: 不应是类 ...
- jQuery Mobile 表单输入元素
jQuery Mobile 文本输入 输入字段是通过标准的 HTML 元素编写的,jQuery Mobile 会为它们设置专门针对移动设备的美观易用的样式.您还可以使用新的 HTML5 <inp ...
- 关于C# DataTable 的一些操作
经常操作DATATABLE 对于一些不需要再通过sql 来重复操作的 可以通过操作datatable来达到同样的效果 方法一: 也是广为人知的一种: YourDataTable.Columns. ...
- 日期函数(sql)
SQL 标量函数----->日期函数 day() .month().year().2009年02月23日 星期一 11:30 SQL 标量函数----->日期函数 day() .month ...
- hibernate基础
另存为查看吧
- SPFA+寻路(行路难,洛谷2832)
啊啊啊这道难题总算是做出来了,首先是帅比浮云的题解发出来一下:http://www.cnblogs.com/fuyun-boy/p/5922742.html 原题目地址:https://www.luo ...
- 这有一个flag
1.并查集[1224] 2.最小生成树?? 3.topsort(好洋气): 4.归并排序[1438]: 5.差分约束系统: 6.A*算法找k短路 7.scanf: 8.搜索[P1198]华容道: 9. ...