一、输入属性(父组件与子组件通信)

1. 创建工程

ng new demo1

2.创建order组件

ng g component order

3. 在order组件里定义输入属性

order组件的html

4. 父组件

app.component.ts中定义stock

app.component.html, 采用双向绑定

效果图

最终父组件IBM的值,通过输入属性,把值传递给了子组件

二、输出属性(子组件与父组件通信)

1. ng g component priceQutoe 创建报价组件

2. 定义报价组件控制器

import {Component, EventEmitter, OnInit, Output} from '@angular/core';

@Component({
selector: 'app-price-quote',
templateUrl: './price-quote.component.html',
styleUrls: ['./price-quote.component.css']
})
export class PriceQuoteComponent implements OnInit { stockCode: string = 'IBM';
price: number; @Output('priceChange')
lastPrice: EventEmitter<PriceQuote> = new EventEmitter(); constructor() {
setInterval(() => {
let priceQuote: PriceQuote = new PriceQuote(this.stockCode, 100 * Math.random());
this.price = priceQuote.lastPrice;
this.lastPrice.emit(priceQuote);
}, 1000);
} ngOnInit() {
} } export class PriceQuote {
constructor(public stockCode: string,
public lastPrice: number) {
}
}

 3. 定义报价组件html

<p>
这里是报价组件
</p>
<div>
股票代码是{{stockCode}}, 股票价格是{{price | number:'2.2-2'}}
</div>

  

4. 父组件控制器

import { Component } from '@angular/core';
import {PriceQuote} from './price-quote/price-quote.component'; @Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent { stock = '';
title = 'app'; priceQuote: PriceQuote = new PriceQuote('', 0); priceQutoehandler(event: PriceQuote){
this.priceQuote = event;
}
}

  

5. 父组件html

<div>
我是父组件
</div>
<app-price-quote (priceChange)="priceQutoehandler($event)"></app-price-quote>
<div>
这是在报价组件外部,股票代码是{{priceQuote.stockCode}},
股票价格是{{priceQuote.lastPrice | number:'2.2-2'}}
</div>

6.效果图

三、中间人模式

当另个组件不是父子组件关系时,需要两个共同的父组件,这个父组件就是中间人模式

中间人模式同时使用了输入属性和输出属性

1. 报价组件定义

import {Component, EventEmitter, OnInit, Output} from '@angular/core';

@Component({
selector: 'app-price-quote',
templateUrl: './price-quote.component.html',
styleUrls: ['./price-quote.component.css']
})
export class PriceQuoteComponent implements OnInit { stockCode: string = 'IBM';
price: number; //@Output('priceChange')
//lastPrice: EventEmitter<PriceQuote> = new EventEmitter() @Output()
buy: EventEmitter<PriceQuote> = new EventEmitter(); constructor() {
setInterval(() => {
const priceQuote: PriceQuote = new PriceQuote(this.stockCode, 100 * Math.random());
this.price = priceQuote.lastPrice;
//this.lastPrice.emit(priceQuote);
}, 1000);
} buyStock(event) {
this.buy.emit(new PriceQuote(this.stockCode, this.price));
} ngOnInit() {
} } export class PriceQuote {
constructor(public stockCode: string,
public lastPrice: number) {
}
}

  

2. 报价组件html

<p>
这里是报价组件
</p>
<div>
股票代码是{{stockCode}}, 股票价格是{{price | number:'2.2-2'}}
</div>
<div>
<input type='button' value='立即购买' (click)="buyStock($event)">
</div>

  

3.订单组件控制器

import {Component, Input, OnInit} from '@angular/core';
import {PriceQuote} from "../price-quote/price-quote.component"; @Component({
selector: 'app-order',
templateUrl: './order.component.html',
styleUrls: ['./order.component.css']
})
export class OrderComponent implements OnInit { @Input()
priceQutoe: PriceQuote; constructor() { } ngOnInit() {
} }

  

4. 订单组件html

<div>
我下单组件
</div>
<div>
买100手{{priceQutoe.stockCode}}股票,买入价为{{priceQutoe.lastPrice | number:'2.2-2'}}
</div>

  

5. 父组件的控制器

import { Component } from '@angular/core';
import {PriceQuote} from './price-quote/price-quote.component'; @Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent { stock = ''; priceQuote: PriceQuote = new PriceQuote('', 0); priceQutoehandler(event: PriceQuote){
this.priceQuote = event;
} buyHandler(event: PriceQuote) {
this.priceQuote = event;
}
}

  

6.父组件的html

<div>
我是父组件
</div>
<app-price-quote (buy)="buyHandler($event)"></app-price-quote>
<app-order [priceQutoe]="priceQuote"></app-order>

  

7.效果图

当点击“立即购买”时,显示当时的显示价格。

Angular 4 组件间的通信的更多相关文章

  1. angular组件间的通信(父子、不同组件的数据、方法的传递和调用)

    angular组件间的通信(父子.不同组件的数据.方法的传递和调用) 一.不同组件的传值(使用服务解决) 1.创建服务组件 不同组件相互传递,使用服务组件,比较方便,简单,容易.先将公共组件写在服务的 ...

  2. 第四节:Vue表单标签和组件的基本用法,父子组件间的通信

    vue表单标签和组件的基本用法,父子组件间的通信,直接看例子吧. <!DOCTYPE html> <html> <head> <meta charset=&q ...

  3. ZeroMQ(java)之I/O线程的实现与组件间的通信

    算是开始读ZeroMQ(java)的代码实现了吧,现在有了一个大体的了解,看起来实现是比较的干净的,抽象什么的不算复杂... 这里先来看看它的I/O线程的实现吧,顺带看看是如何实现组件的通信的.... ...

  4. vuejs单一事件管理组件间的通信

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. React中父子组件间的通信问题

    1.https://blog.csdn.net/sinat_17775997/article/details/59103173 (React中父子组件间的通信问题)

  6. Vue2不使用Vuex如何实现兄弟组件间的通信

    在一些正规的大型项目的企业级开发过程中我们一般会引入Vuex来对Vue所有组件进行状态管理,可以轻松实现各组件间的通信.但是有时候做做自己的小项目,没有必要使用Vuex时,如何简单的实现组件间的通信? ...

  7. (尚033)Vue_案例_slot(组件间的通信4:slot)

    1.组件间的通信4:slot(slot:插槽,就是一个占位) slot用于标签反复使用很多次 1.1理解 此方式用于父组件向子组件传递标签数据, 其他为数据通信 外面组件向里面组件传递标签进去,直接拿 ...

  8. 后端学 Angular 2 —— 组件间通信

    1. 父组件向子组件传递信息 使用@Input 子组件的属性用 @Input 进行修饰,在父组件的模板中绑定变量 例子: import { Component, OnInit, Input } fro ...

  9. vue组件间的通信

    组件的定义: 组件(Component)是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素, Vue.js 的编译器为它添加特殊功能.v ...

随机推荐

  1. 使用 PM2 管理nodejs进程

    pm2 是一个带有负载均衡功能的Node应用的进程管理器. 当你要把你的独立代码利用全部的服务器上的所有CPU,并保证进程永远都活着,0秒的重载, PM2是完美的. 它非常适合IaaS结构,但不要把它 ...

  2. OC MRC之计数器的基本操作(代码分析)

    /* 1.方法的基本使用 1> retain :计数器+1,会返回对象本身 2> release :计数器-1,没有返回值 3> retainCount :获取当前的计数器 4> ...

  3. 清除mac出现的.DS_Store文件

    一.什么是.DS_Store文件 在 Mac OS X 系统下,大部分文件夹中都包含 .DS_Store 隐藏文件,这里保存着针对这个目录的特殊信息和设置配置,例如查看方式.图标大小以及这个目录的一些 ...

  4. mongodb控制台中文乱码

    问题描述: 使用命令行打开mongo,查询的结果里中文都是乱码,检查了文件编码均正常: 解决方法: 该问题是cmd字体引起的,设置cmd的字体即可,cmd的默认字体是“点阵字体”,选择其他两个均可,如 ...

  5. NOIP初赛 BLESS ALL!

    祝初赛顺利!RP++! 下午再写一篇题解来加RP

  6. learning docker steps(5) ----- docker stack 初次体验

    参考:https://docs.docker.com/get-started/part5/ stack 技术栈.技术栈是一组相关的服务,它们共享依赖项并且可以一起进行编排和扩展.单个技术栈能够定义和协 ...

  7. learning docker steps(4) ----- docker swarm 初次体验

    参考:https://docs.docker.com/get-started/part4/ 了解 swarm 集群 swarm 是一组运行 Docker 并且已加入集群中的机器.执行此操作后,您可以继 ...

  8. Scrapy-redis改造scrapy实现分布式多进程爬取

    一.基本原理: Scrapy-Redis则是一个基于Redis的Scrapy分布式组件.它利用Redis对用于爬取的请求(Requests)进行存储和调度(Schedule),并对爬取产生的项目(it ...

  9. docker中进行IDA远程调试提示“TRACEME: Operation not permitted[1] Closing connection from 192.168.109.1...”的解决方法

    加入 --security-opt seccomp:unconfined选项,关闭docker远程命令执行保护 如: docker run --security-opt seccomp:unconfi ...

  10. python3:定时执行自动化测试脚本

    转载请注明出处:https://www.cnblogs.com/shapeL/p/9172990.html 1.windows任务计划定时执行脚本 (1)创建 .bat 文件,执行脚本的命令(inte ...