*/

.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 (服务)的更多相关文章

  1. android学习日记19--四大组件之Services(服务)

    一个Android应用主要由四个基本组件组成,Android四大基本组件分别是Activity,Content Provider内容提供者,Service服务,BroadcastReceiver广播接 ...

  2. PHP 调用asp.net Web Services服务问题总结

    原文:PHP 调用asp.net Web Services服务问题总结 PHP是弱类型语言,转换非常不方便. < ?php //soap 客户端 $client=new SoapClient(' ...

  3. Integration Services 服务连接失败,拒绝访问以及无法检索数据报错问题

    第一个方法比较简单:把域账号添加admin组即可: 第二种方法: 添加域账号到分布式 COM 组 命令提示符下运行 dcomcnfg.exe 下一步 下一步 启动和激活权限 下一步 访问权限 同上设置 ...

  4. Power BI 与 Azure Analysis Services 的数据关联:1、建立 Azure Analysis Services服务

    Power BI 与 Azure  Analysis Services 的数据关联:1.建立  Azure  Analysis Services服务

  5. 为何使用Microsoft SQL Server Management Studio连接Integration Services服务失败

    检查是否满足以下各项: 1. 首先你要确保当前你使用的Windows账号是有管理员权限的 2. 其次请在打开Microsoft SQL Server Management Studio时,通过右键Ru ...

  6. HanLP封装为web services服务的过程介绍

    前几天的召开的2019年大数据生态产业大会不知道大家关注到没有,看到消息是hanlp2.0版本发布了.不知道hanlp2.0版本又将带来哪些新的变化?准备近期看能够拿到一些hanlp2.0的资料,如果 ...

  7. Windows server 2008启动remote dosktop services服务报错1079

    原创 欢迎转载,届时请注明出处 报错场景 今天,刚开始启动win server的远程连接服务(remote desktop services)时 该服务可以正常启动,然后选择了  “计算机--> ...

  8. 1.3 Services - 服务

    服务是一种应用组件,它可以在后台执行耗时的操作,它是没有用户界面的.其它的应用组件都可以开启一个服务,服务开启后,即使用户离开了应用,服务仍然可以在后台运行.此外,绑定到服务的组件可以与服务进行交互, ...

  9. 基于soap 的 python web services 服务开发指南

    文章大纲 序言 相关概念 SOA web services SOAP WSDL UDDI 环境搭建 我们使用 python 3.6 这个较新python 版本 服务端开发 客户端开发 suds-jur ...

随机推荐

  1. Docker 不能被外网正常访问

    问题描述: 1. docker容器不能 被外部网络正常访问 2. docker所在服务器可以正常访问 3. docker启动时 报 ip_forward 错误 ARNING: IPv4 forward ...

  2. form表单reset表格并执行搜索

    其中reset() 不需要定义 search():是你执行的搜索的函数 <html> <head> <title>sf</title></head ...

  3. man curl_easy_setopt(原创)

    中文翻译: curl_easy_setopt(3) libcurl 手册 curl_easy_setopt(3) 名称 curl_easy_setopt -curl的设置选项概要 #include & ...

  4. Ubuntu 杂音 alsa*

    $ sudo alsactl init # 初始化 $ sudo alsactl store # 存储状态 会调的话可以 $ alsamixer

  5. ie,火狐,谷歌 select清除默认样式 设置新的样式

    select { border: solid 1px #000; /*Chrome和Firefox里面的边框是不一样的,所以复写了一下*/ appearance:none; -moz-appearan ...

  6. call

    -------siwuxie095 call 调用另一个批处理程序或自身程序段,调用完,程序会回到原来 call 的地方继续执行 如果在脚本或批处理文件外使用 call,则不会在命令行起作用 语法 c ...

  7. [问题]数据库MySQL和Navicat的乱码问题

    计算机中存储字符需要使用编码集,早期有ASCII集,但是随着技术的发展,ASCII集不能满足需求,出现了越来越多的字符,比如中文字符等.后来又发展出了Unicode.GB2312.utf8等字符集.字 ...

  8. css3新特性@media(媒体查询)

    现在web页面运行的设备的屏幕从pc端到pad,从pad到手机,各种各样,这样就用到了现在经常用到的响应式页面,为实现响应式页面开发,就用到一个非常有用的css3属性media(媒体查询). 介绍:媒 ...

  9. STL学习笔记

    简介 STL(Standard Template Library),即标准模版库,涵盖了常用的数据结构和算法,并具有跨平台的特点.STL是C++标准函数库的一部分,如下图所示: STL含有容器.算法和 ...

  10. Java hashCode() 和 equals()的若干问题

    原文:http://www.cnblogs.com/skywang12345/p/3324958.html 本章的内容主要解决下面几个问题: 1 equals() 的作用是什么? 2 equals() ...