shared-service.ts
shared-service.ts
import { Observable } from 'rxjs/Observable';
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class SharedService {
// Observable string sources
private emitChangeSource = new Subject<any>();
// Observable string streams
changeEmitted$ = this.emitChangeSource.asObservable();
// Service message commands
emitChange(change: any) {
this.emitChangeSource.next(change);
}
}
Now inject the instance of the above service in the constructor of both the parent and child component.
The child component will be emitting a change every time the onClick() method is called
child.component.ts
import { Component} from '@angular/core';
@Component({
templateUrl: 'child.html',
styleUrls: ['child.scss']
})
export class ChildComponent {
constructor(
private _sharedService: SharedService
) { }
onClick(){
this._sharedService.emitChange('Data from child');
}
}
The parent component shall receive that change. To do so,capture the subscription inside the parent's constructor.
parent.component.ts
import { Component} from '@angular/core';
@Component({
templateUrl: 'parent.html',
styleUrls: ['parent.scss']
})
export class ParentComponent {
constructor(
private _sharedService: SharedService
) {
_sharedService.changeEmitted$.subscribe(
text => {
console.log(text);
});
}
}
Hope this helps :)
shared-service.ts的更多相关文章
- angular6 http.service.ts
import { Injectable, isDevMode } from '@angular/core'; import { HttpClient, HttpParams, HttpHeaders ...
- angular2+ 组件间通信
angular2+ 不同于react的redux,vue的vuex,angular2+其实可实现数据状态管理的方法很多,以下方案一般也足够支撑普通业务: 父子组件通信 1.1 父组件向子组件传递信息( ...
- Angular2 Service实践——实现简单音乐播放服务
引言: 如果说组件系统(Component)是ng2应用的躯体,那把服务(Service)认为是流通于组件之间并为其带来生机的血液再合适不过了.组件间通信的其中一种优等选择就是使用服务,在ng1里就有 ...
- Angular2 Service实践
引言: 如果说组件系统(Component)是ng2应用的躯体,那把服务(Service)认为是流通于组件之间并为其带来生机的血液再合适不过了.组件间通信的其中一种优等选择就是使用服务,在ng1里就有 ...
- Angular 非父子组件间的service数据通信
完成思路:以service.ts(主题subject---订阅sbuscribe模式)为数据中转中间件,通过sku.ts的数据更改监测机制,同步更改service.ts中的数据,同时buy.ts组件实 ...
- Ionic3 新增 Service
service是单例模式的 新增Service类 search.service.ts import {Injectable} from '@angular/core'; @Injectable() e ...
- 一些angular/js/ts的坑和吐槽
------20190318 ------------- 回头看,很多槽点已经随着升级改掉了 绑定string字面值到子组件@Input <app-overlay-static [name] ...
- angularcli 第七篇(service 服务)
在组件中定义的信息是固定的,假设另外一个组件也需要用到这些信息,这时候就用到服务,实现 共享数据 和 方法 组件不应该直接获取或保存数据,它们不应该了解是否在展示假数据. 它们应该聚焦于展示数据,而把 ...
- Angular:使用service进行http请求的简单封装
①使用ng g service services/storage创建一个服务组件 ②在app.module.ts 中引入HttpClientModule模块 ③在app.module.ts 中引入创建 ...
- asp.net c# 网上搜集面试题目大全(附答案)
1.String str=new String("a")和String str = "a"有什么区别? String str = "a"; ...
随机推荐
- Sys.dm_os_wait_stats Sys.dm_performance_counters
wait_type waiting_tasks_count wait_time_ms max_wait_time_ms signal_wait_time_ms MISCELLANEOUS 0 0 0 ...
- 深入学习 History 对象管理浏览器会话历史
History对象允许我们操作浏览器会话历史,即加载当前页面的标签页窗口或frame窗口的访问历史.之前有同学咨询我如何实现拦截用户跳转页面并强制用户返回首页后重新请求页面,于是有了本篇博客的主题,本 ...
- Shared libraries with GCC on Linux
转:http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html By anduril462 Libraries are a ...
- Thunderbird for Ubuntu
转自:http://www.cnblogs.com/slave_wc/archive/2011/05/02/2034529.html 装好ubuntu 的一般基本配置见本博客另一篇文章: Ubun ...
- 分公司下拉框赋值-从后台传到前端jsp
我的旧代码 List<MetaBranchCfg> list = metaBranchCfgBO.queryAllBranchList(); request.setAttribute( ...
- 《Go语言实战》笔记之协程同步 sync.WaitGroup
原文地址(欢迎互换友链): http://www.niu12.com/article/8 sync 包提供同步 goroutine 的功能 <p>文档介绍</p><cod ...
- HTML 标签的 enctype 属性
定义和用法 enctype 属性规定在发送到服务器之前应该如何对表单数据进行编码. 默认地,表单数据会编码为 "application/x-www-form-urlencoded" ...
- TCP三次握手连接和TCP四次挥手及大量TIME_WAIT解决方法:
1.TCP建立连接,三次握手 建立的TCP连接可靠的连接,必须经过三次握手建立连接才能正式通信彼此传输数数据. 客户端请求服务端建立连接 第一次握手:客户给服务发送一个请求报文SYN, 客户端的状态置 ...
- [转]SSIS高级转换任务—行计数
本文转自:http://www.cnblogs.com/tylerdonet/archive/2011/06/19/2084780.html 在SSIS中的Row Count转换可以在数据流中计算数据 ...
- iOS: 常用的宏
Github地址:https://github.com/XFZLDXF/Macro/blob/master/MacroDefinition.h // // MacroDefinition.h // M ...