[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 ...
随机推荐
- thymeleaf 引入js css 无效
转自:https://blog.csdn.net/qq_33833327/article/details/81388502
- 18.29SSM基础整合开发
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/d ...
- ffmpeg实现
最近做一个小项目,要在线播放录制的 MP4 视频,想开源的 flash player 或 html 5 可以播放.可,虽然 MP4 是 H.264 编码,但就是播放不了.可能是封装方式(PS 方式)不 ...
- Hdu-6252 2017CCPC-Final J.Subway Chasing 差分约束
题面 题意:有2个人,都去坐地铁,但是他们相差了X分钟,但是他们也一直在通讯,于是你就知道,你在AB站点中间的时候,他在CD中间,(B一定等于A+1或者A,同理D也是),问你每2个站之间需要的时间的一 ...
- 基于scrapy-redis组件的分布式爬虫
scrapy-redis组件安装 分布式实现流程 scrapy-redis组件安装 - 下载scrapy-redis组件:pip install scrapy-redis - 更改redis配置文件: ...
- Elasticsearch之curl创建索引库和索引时注意事项
前提, Elasticsearch之curl创建索引库 Elasticsearch之curl创建索引 注意事项 1.索引库名称必须要全部小写,不能以下划线开头,也不能包含逗号 2.如果没有明确指定索引 ...
- SQLServer 添加序号列
select ROW_NUMBER()OVER(ORDER BY 用来排序的列的列名),XXX,XXX from XXX 按照原始顺序: ROW_NUMBER()OVER(ORDER BY (sele ...
- RabbitMQ学习之队列监控
对于RabbitMQ的监控,除了服务器基本信息(硬盘.CPU.内存.IO等)以及MQ的进程和端口,我们也可以通过请求url访问管理API监控其集群和队列的情况.在Javaapi 3.6.0以后,cha ...
- oc懒加载 & swift lazy
oc的懒加载依赖于属性的双重属性的函数属性部分. 懒加载的本质是执行get函数. swift的lazy,理论上与此类似. 编译器优化时可能对初始化块进行了保存. 懒加载的本质是延迟执行. 只要是执行, ...
- Pyhhon中一些常见的字符串操作.
可变变量:list, 字典 不可变变量:元祖,字符串 字符串的操作(去掉空格, 切片, 查找, 连接, 分割, 转换首字母大写, 转换字母大小写, 判断是否是数字字母, 成员运算符(in / not ...