An Angular service registered on the NgModule is globally visible on the entire application. Moreover it is a singleton instance, so every component that requires it via its constructor, will get the same instance. However this is not always the desired behavior.

Rather you may like to get a fresh instance of a given service every time a component requests it. This is especially powerful for caching scenarios for instance. In this lesson we will learn how to achieve such behavior with Angular’s hierarchical dependency injector.

It menas that, when you provide dependency injection for a component, it comes and goes with Component, which also mean, everytime component is renewed, the service provides init value, the old value will lose.

@Component({
selector: 'app-child',
template: `
<h3>Child component</h3>
<pre>{{ personService.getPerson() | json }}</pre> <app-person-edit></app-person-edit>
`,
providers: [PersonService]
})
import { Injectable } from '@angular/core';

@Injectable()
export class PersonService {
name = 'Joe'; getPerson() {
return {
name: this.name,
age: 23
};
} setPersonName(value) {
this.name = value;
}
}

So everytime the component's will get serivce name as 'Joe'.

[Angular] Component's dependency injection的更多相关文章

  1. [Angular Tutorial] 7-XHRs & Dependency Injection

    我们受够了在应用中用硬编码的方法嵌入三部电话!现在让我们用Angular内建的叫做$http的服务来从我们的服务器获取更大的数据集吧.我们将会使用Angular的依赖注入来为PhoneListCtrl ...

  2. [Angular] Communicate Between Components Using Angular Dependency Injection

    Allow more than one child component of the same type. Allow child components to be placed within the ...

  3. AngularJs学习笔记--Dependency Injection(DI,依赖注入)

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/di 一.Dependency Injection(依赖注入) 依赖注入(DI)是一个软件设计模式,处理 ...

  4. MVC Controller Dependency Injection for Beginners【翻译】

    在codeproject看到一篇文章,群里的一个朋友要帮忙我翻译一下顺便贴出来,这篇文章适合新手,也算是对MEF的一个简单用法的介绍. Introduction In a simple stateme ...

  5. [转载][翻译] IoC 容器和 Dependency Injection 模式

    原文地址:Inversion of Control Containers and the Dependency Injection pattern 中文翻译版本是网上的PDF文档,发布在这里仅为方便查 ...

  6. Inversion of Control Containers and the Dependency Injection pattern(转)

    In the Java community there's been a rush of lightweight containers that help to assemble components ...

  7. 【译】Dependency Injection with Autofac

    先说下为什么翻译这篇文章,既定的方向是架构,然后为了学习架构就去学习一些架构模式.设计思想. 突然有一天发现依赖注入这种技能.为了使得架构可测试.易维护.可扩展,需要架构设计为松耦合类型,简单的说也就 ...

  8. 依赖注入 | Dependency Injection

    原文链接: Angular Dependency Injection翻译人员: 铁锚翻译时间: 2014年02月10日说明: 译者认为,本文中所有名词性的"依赖" 都可以理解为 & ...

  9. Inversion of Control Containers and the Dependency Injection pattern

    https://martinfowler.com/articles/injection.html One of the entertaining things about the enterprise ...

随机推荐

  1. thinkphp 具体常量,在view里面使用

    1 2 3 4 5 6 7 8 9 '__TMPL__'      =>  APP_TMPL_PATH,  // 项目模板目录 '__ROOT__'      =>  __ROOT__,  ...

  2. C#中的流_字节_字符_字符串之间的相互转换

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  3. LUA 创建文件和文件夹

    创建文件: os.execute('mkdir e:\\aa') 创建文件夹: os.execute("cd.>e:\\wang.ini")

  4. VM虚拟机NAT模式主机与虚拟机ping不通解决方案

    VM虚拟机与真机通信三种模式, 桥接模式,NAT 模式 ,HOST-ONLY 模式. NAT模式 使用虚拟机的一个虚拟网卡做NAT网关,在nat网关上配dhcp ,或者直接用静态地址.就相当于形成了一 ...

  5. Boost锁~临界区保护和临界资源共享

    前言: 除了thread,boost::thread另一个重要组成部分是mutex,以及工作在mutex上的boost::mutex::scoped_lock.condition和barrier,这些 ...

  6. 模拟试题C

    模拟试题C 一.单项选择题(2′*14 =28′) 1.双线性法向插值法(Phong Shading)的优点是( ) A)法向计算精确 B)高光域准确 C)对光源和视点没有限制 D)速度较快 2.用编 ...

  7. 1 ERP管理系统概念

    1 ERP管理系统概念 一.ERP是什么? ERP是企业资源计划(Enterpise Resource Planning)的简称,蕴含现代企业管理理念,其核心是在制造资源计划基础上进一步发展而成的面向 ...

  8. (转)shiro权限框架详解05-shiro授权

    http://blog.csdn.net/facekbook/article/details/54910606 本文介绍 授权流程 授权方式 授权测试 自定义授权realm 授权流程 开始构造Secu ...

  9. python tips:文件读取——换行符的问题

    问题:在windows系统中,换行的符号是'\r\n'.python在读文件的时候为了系统兼容,会默认把'\r','n','\r\n'都视作换行.但是在windows文件中,可能在同一行中同时存在'\ ...

  10. struts中日期处理以及文件下载

    日期处理 对于jsp提交的基本数据类型和日期格式为yyyy-MM-dd的自动转换为相应的 对于其它的日期格式需要自定义转换器 局部类型转换器 1,写转换器类(继承StrutsTypeConverter ...