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'.

最新文章

  1. PHP的字符串函数
  2. Beeline known issues
  3. linux双机GFS的配置
  4. Mininet 创建topo的时候指定host的ip
  5. poj 2081 Recaman&amp;#39;s Sequence
  6. stm32之RCC
  7. FineUI Grid 缓存列显示隐藏状态
  8. 【LeetCode】459. Repeated Substring Pattern
  9. border三角形阴影(不规则图形阴影)和多重边框的制作
  10. 2018.11.30开始学习shader
  11. 114、drawable和mipmap 目录下图片的区别
  12. linux4.10.8 内核移植(一)---环境搭建及适配单板。
  13. android用TextView实现跑马灯效果
  14. [转载]Request、Request.Form和Request.QueryString的区别
  15. centos7数据库连接使用127.0.0.1报permission denied,使用localhost报No such file or directory
  16. git reset与git revert比較
  17. pixi.js 总结
  18. HDU 3342 Legal or Not(拓扑排序判断成环)
  19. CXF和Axis2开发webservice也是可以添加asmx等后缀
  20. javascript基础语法及使用

热门文章

  1. XML基础+Java解析XML +几种解析方式的性能比较
  2. Mysql数据库性能
  3. mysql 从库落后主库太多优化
  4. 运维自动化-Ansible
  5. Liunx搜索命令行
  6. HBase编程 API入门系列之modify(管理端而言)(10)
  7. resgen.exe 已退出 代码为 1073741701的错误的解决办法
  8. Android学习——Button填充颜色及实现圆角
  9. 基于S3C2440数码相框
  10. 遍历及过滤 first(), last() 和 eq() filter() 和 not()