*/

.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. Java for循环的几种用法

    J2SE 1.5提供了另一种形式的for循环.借助这种形式的for循环,可以用更简单地方式来遍历数组和Collection等类型的对象.本文介绍使用这种循环的具体方式,说明如何自行定义能被这样遍历的类 ...

  2. Maven Archetype

    1. 从project创建archetype 在项目根目录下,运行 mvn archetype:create-from-project 创建的archetype工程在app_folder/target ...

  3. SQL Server更新表(用一张表的数据更新另一张表的数据)

    a) 写法轻松,更新效率高: update table1 set field1=table2.field1,field2=table2.field2 from table2 where table1. ...

  4. char、varchar、nchar、nvarchar的区别

    http://www.cnblogs.com/mekong/archive/2009/04/17/1437996.html

  5. 第一章 删掉centos原有的openjdk并安装sun jdk

    一.卸载原有openjdk rpm -qa | grep java 之后,将展示出来的全部卸载掉,我这里是5个 rpm -e --nodeps java-1.7.0-openjdk-1.7.0.111 ...

  6. POI读取Excel内容格式化

    在用POI读取Excel内容时,经常会遇到数据格式化的问题. 比如:数字12365会变为12365.0;字符串数字123也会变为123.0,甚至会被变为科学计数法.另外日期格式化也是一个头疼的问题.其 ...

  7. ImageUtil(验证码数据生成工具类)

    登录界面时,常常会使用到验证码图片生成,程序如下: package util; import java.awt.Color; import java.awt.Font; import java.awt ...

  8. iOS实现类似于歌词进度效果

    先看效果 这里关键的地方在于镂空文字的实现,可以用UILabel的drawRect方法. .h文件 @interface HollowLabel : UILabel @end .m文件 @interf ...

  9. js 基本类型与引用类型的区别

    1. 基本类型: string,number,boolean,null,undefined 2. 引用类型: Function,Array,Object 访问方式                   ...

  10. 去除inline-block元素间间距,比较靠谱的两种办法

    1.使用注释符号 <div><span class="1">1</span></div><!-- --><div& ...