[Angular] Component's dependency injection
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的更多相关文章
- [Angular Tutorial] 7-XHRs & Dependency Injection
我们受够了在应用中用硬编码的方法嵌入三部电话!现在让我们用Angular内建的叫做$http的服务来从我们的服务器获取更大的数据集吧.我们将会使用Angular的依赖注入来为PhoneListCtrl ...
- [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 ...
- AngularJs学习笔记--Dependency Injection(DI,依赖注入)
原版地址:http://code.angularjs.org/1.0.2/docs/guide/di 一.Dependency Injection(依赖注入) 依赖注入(DI)是一个软件设计模式,处理 ...
- MVC Controller Dependency Injection for Beginners【翻译】
在codeproject看到一篇文章,群里的一个朋友要帮忙我翻译一下顺便贴出来,这篇文章适合新手,也算是对MEF的一个简单用法的介绍. Introduction In a simple stateme ...
- [转载][翻译] IoC 容器和 Dependency Injection 模式
原文地址:Inversion of Control Containers and the Dependency Injection pattern 中文翻译版本是网上的PDF文档,发布在这里仅为方便查 ...
- 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 ...
- 【译】Dependency Injection with Autofac
先说下为什么翻译这篇文章,既定的方向是架构,然后为了学习架构就去学习一些架构模式.设计思想. 突然有一天发现依赖注入这种技能.为了使得架构可测试.易维护.可扩展,需要架构设计为松耦合类型,简单的说也就 ...
- 依赖注入 | Dependency Injection
原文链接: Angular Dependency Injection翻译人员: 铁锚翻译时间: 2014年02月10日说明: 译者认为,本文中所有名词性的"依赖" 都可以理解为 & ...
- Inversion of Control Containers and the Dependency Injection pattern
https://martinfowler.com/articles/injection.html One of the entertaining things about the enterprise ...
随机推荐
- x64汇编第三讲,64位调用约定与函数传参.
目录 x64汇编第三讲,64位调用约定与函数传参. 一丶复习X86传参 二丶x64汇编 2.1汇编详解 x64汇编第三讲,64位调用约定与函数传参. 一丶复习X86传参 在x86下我们汇编的传参如下: ...
- flex和layout移动端布局
1.九宫格 样式为: ul{ display: flex; flex-wrap: wrap;//超出换行 } li{ width: 33%; height: 60px; display: flex; ...
- 读入图片显示scipy.misc module has no attribute imread?
>>> import scipy >>> scipy.misc <module 'scipy.misc' from 'C:\Python27\lib\site ...
- nfs server and client
系统环境:Centos7.3 ip1:10.0.0.1 ip2:10.0.0.2 检测是否安装NFS服务 rpm -qa | grep nfs rpm -qa|grep nfs 安装NFS ...
- JavaWeb详细学习路线图
- Java攻城狮学习路线 - 图转自网络.
- 09.javaweb简单标签编程
一.简单标签 1, 简介:由于传统标签使用三个标签接口来完成不同的功能,显得过于繁琐,不利于标签技术的推广, SUN公司为降低标签技术的学习难度,在JSP 2.0中定义了一个更为简单.便于编写和调用 ...
- readonly and const variable
共同点:都是常量: 不同点:const的值必须在编译前确定,通常在声明的同时赋值:而readonly可在运行时确定:
- redis的跳跃表
跳跃表是一种插入.查询.删除的平均时间复杂度为O(nlogn)的数据结构,在最差情况下是O(n),当然这几乎很难出现. 和红黑树相比较 最差时间复杂度要差很多,红黑树是O(nlogn),而跳跃表是O( ...
- 认识图片放大工具PhotoZoom的菜单栏
使用PhotoZoom能够对数码图片无损放大,备受设计师和业内人员的青睐,它的出现时一场技术的革新,新颖的技术,简单的界面,优化的算法,使得它可以对图片进行放大而没有锯齿,不会失真.本文为您一起来认识 ...
- H3C交换机telnet服务认证模式配置
以H3C交换机为例,介绍telnet服务的三种认证方式配置(none无需认证,password密码认证,scheme账户+密码认证) None认证模式配置步骤:[H3C]telnet server e ...