angular 输入属性@Input , 输出属性@Output , 中间人模式
1 输入属性
通常用于父组件向子组件传递信息
举个栗子:我们在父组件向子组件传递股票代码,这里的子组件我们叫它app-order
首先在app.order.component.ts中声明需要由父组件传递进来的值
order.component.ts
...
@Input()
stockCode: string @Input()
amount: string
...
order.component.html
<p>这里是子组件</p>
<p>股票代码为{{stockCode}}</p>
<p>股票总数为{{amount}}</p>
然后我们需要在父组件(app.component)中向子组件传值
app.component.ts
...
stock: string
...
app.component.html
<input type="text" placeholder="请输入股票代码" [(ngModel)]="stock"> <app-order [stockCode]="stock" [amount]="100"></app-order>
这里我们使用了Angular的双向数据绑定,将用户输入的值和控制器中的stock进行绑定。然后传递给子组件,子组件接收后在页面显示。
2 输出属性
当子组件需要向父组件传递信息时需要用到输出属性。
举个栗子:当我们从股票交易所获得股票的实时价格时,希望外部也可以得到这个信息。为了方便,这里的实时股票价格我们通过一个随机数来模拟。这里的子组件我们叫它app.price.quote
使用EventEmitter从子组件向外发射事件
price.quote.ts
export class PriceQuoteComponent implements OnInit{
stockCode: string = 'IBM';
price: number; //使用EventEmitter发射事件
//泛型是指往外发射的事件是什么类型
//priceChange为事件名称
@Output()
priceChange:EventEmitter<PriceQuote> = new EventEmitter(); constructor(){
setInterval(() => {
let priceQuote = new PriceQuote(this.stockCode, 100*Math.random());
this.price = priceQuote.lastPrice;
//发射事件
this.priceChange.emit(priceQuote);
})
} ngInit(){
}
} //股票信息类
//stockCode为股票代码,lastPrice为股票价格
export class PriceQuote{
constructor(public stockCode:string,
public lastPrice:number
)
}
price.quote.html
<p>
这里是报价组件
</p>
<p>
股票代码是{{stockCode}}
</p>
<p>
股票价格是{{price | number:'2.2-2'}}
</p>
接着我们在父组件中接收事件
app.component.html
<app-price-quote (priceChange)="priceQuoteHandler($event)"></app-price-quote> <div>
这是在报价组件外, 股票代码是{{priceQuote.stokcCode}},
股票价格是{{priceQuote.lastPrice | number:'2.2-2'}}
</div>
事件绑定和原生的事件绑定是一样的,都是将事件名称放在()中。
app.component.ts
export class AppComponent{
priceQuote:PriceQuote = new PriceQuote('', 0); priceQuoteHandler(event:PriceQuote){
this.priceQuote = event;
}
}
这里的event类型就是子组件传递事件的类型。
简单的说,就是子组件通过emit发射事件priceChange,并将值传递出来,父组件在使用子组件时会触发priceChange事件,接收到值。
3 中间人模式
组件之间可以通过建立父子关系来传递数据,但是这样两个组件之间的耦合性太强,重用性太低。那么如果组件间不存在关系,可以传递数据吗?如何传递呢?
中间人模式,顾名思义,就是两个组件之间通过一个中间人来传递数据,两个组件之间不需要知道彼此的存在,中间人接收一个组件的数据传递给另一个组件。
场景
交易员监看报价组件的价格,但股票的价格达到某一个值的时候,交易员会点一个交易按钮来购买股票,报价组件通知中间人交易员要购买股票,中间人知道哪个组件可以完成下单,并将股票价格传递该组件
实现
1、报价组件
(1) 添加购买按钮
<div>
我是报价组件
</div>
<div>
股票代码是{{stockCode}},股票价格是{{price | number:'2.2-2'}}
</div> <div>
<input type="button" value="立即购买" (click)="buyStock($event)">
</div>
(2) 然后报价组件将当前股票价格发射出去
//用来发射报价
@Output()
buy:EventEmitter<PriceQuote> = new EventEmitter(); constructor() {
setInterval(() => {
// 声明一个priceQuote变量
let priceQuote:PriceQuote = new PriceQuote(this.stockCode, *Math.random());
this.price = priceQuote.lastPrice;
// 用emit方法发射事件的时候,就是这个泛型<PriceQuote>所制定的变量priceQuote的数据
this.lastPrice.emit(priceQuote);
},)
} //这就是报价组件做的事,只要把价格发射出去就行,不管谁去接收(应该是中间人去接收,也就是app组件)
//点击按钮时,用buy.emit把当前股票价格发射出去
buyStock(event){
this.buy.emit(new PriceQuote(this.stockCode,this.price)); }
2、中间人接收(app组件),并传给下单组件
<!-- 监听buy事件,并接收数据,然后通过属性绑定传给下单组件 -->
<app-price-quote (buy)="buyHandler($event)"></app-price-quote>
<app-order [priceQuote]='priceQuote'></app-order> export class AppComponent { stock = "";
//给本地的priceQuote设置默认值
priceQuote:PriceQuote = new PriceQuote("",); // 在priceQuoteHandler方法中接收event,event类型就是PriceQuote类型的(子组件中发射出的类型)
buyHandler(event:PriceQuote){
// 然后让本地的priceQuote等于捕获的event
this.priceQuote = event;
}
}
3、下单组件,接收中间人传来的数据,并显示
(1) 接收
@Input()
priceQuote:PriceQuote;
(2) 显示
<div>
我是下单组件
</div>
<div>
<!-- 绑定属性 -->
卖100手{{priceQuote.stockCode}}股票,买入价格是{{priceQuote.lastPrice | number:'2.2-2'}}
</div>
效果显示
点击“立即购买”按钮,下单组件就可以接收到报价组件的实时信息。
小结
这样中间人模式就实现了,两个组件之间不需要指导彼此的存在就可以传递数据,增强组件的重用性。
angular 输入属性@Input , 输出属性@Output , 中间人模式的更多相关文章
- Angular2 组件与模板 -- 输入和输出属性
Input and Output properties 输入属性是一个带有@Input 装饰器的可设置属性,当它通过属性绑定的形式被绑定时,值会"流入"到这个属性. 输出属性是一个 ...
- java—数组乘积输入: 一个长度为n的整数数组input 输出: 一个长度为n的数组result,满足result[i] = input数组中,除了input[i] 之外的所有数的乘积,不用考虑溢出例如 input {2, 3, 4, 5} output: {60, 40, 30, 24}
/** * 小米关于小米笔试题 数组乘积输入: 一个长度为n的整数数组input 输出: 一个长度为n的数组result,满足result[i] = * input数组中,除了input[i] 之外的 ...
- arcgis api for javascript 学习(三) 调用发布地图信息,并将地图属性信息输出到Excel表中
吐血推荐:网上搜了很久关于webgis地图属性表输出到Excel表,并没能找到相关有价值的信息,在小白面前,这就是一脸懵x啊!网上要么是关于前端如何在页面上直接导出excel,和webgis半毛钱关系 ...
- HTML中强大的input标签属性
用了许久的html,<input>这个标签是最常用的标签之一. <input type="">标签中type属性是必不可少的,以往我最常用的有 type=& ...
- HTML 5 <input> placeholder 属性
原文链接:http://www.w3school.com.cn/html5/att_input_placeholder.asp HTML 5 <input> placeholder 属性 ...
- HTML5 INPUT新增属性
HTML5的input标签新增了很多属性,也是让大家非常兴奋的一件事,用简单的一个属性搞定以前复杂的JS验证.input新增的这些属性,使得html和js的分工更明确了,使用起来十分舒畅.我们先看下i ...
- 改进《完美让IE兼容input placeholder属性的jquery实现》的不完美
<完美让IE兼容input placeholder属性的jquery实现>中的代码在IE9以下浏览器中会出错,原因是因为ie9以下的浏览器对input的标签的解释不同. 例如对以下文本框的 ...
- tensorflow笔记6:tf.nn.dynamic_rnn 和 bidirectional_dynamic_rnn:的输出,output和state,以及如何作为decoder 的输入
一.tf.nn.dynamic_rnn :函数使用和输出 官网:https://www.tensorflow.org/api_docs/python/tf/nn/dynamic_rnn 使用说明: A ...
- Hmtl5 <input>中placeholder属性(新属性)
Hmtl5 <input>中placeholder属性(新属性) 一.定义和用法 placeholder 属性提供可描述输入字段预期值的提示信息(hint). 该提示会在输入字段为空时显示 ...
随机推荐
- 手写Json转换
在做项目的时候总是要手动将集合转换成json每次都很麻烦,于是就尝试着写了一个公用的方法,用于转换List to json: using System; using System.Collection ...
- Android RxJava/RxAndroid结合Retrofit使用
概述 RxJava是一个在 Java VM 上使用可观測的序列来组成异步的.基于事件的程序的库.更重要的是:使用RxJava在代码逻辑上会非常简洁明了,尤其是在复杂的逻辑上.告别迷之缩进. RxAnd ...
- actor中!(tell)与forward的差别
! 的源代码: def !(message: Any)(implicit sender: ActorRef = Actor.noSender): Unit tell 的源代码: final def t ...
- RocketMq通信协议格式及编解码 (源码分析)
一.RocketMq broker服务器与客户端的网络通信是基于netty4.x实现的,重点分析 RocketMq设计的通信协议及对应的编解码 开发. 名字解释 ...
- js中,{}初始化数据类型object;for in 的用法;delete的用法
var choices = {}; //此数据表示的是:object{} for(var i=0;i<10;i++){ choices[i+1] = [data[i].testPlan,test ...
- python3带参数的装饰器 函数参数类型检查
from inspect import signature#python3才有的模块 def typeassert(*args,**kwargs): def decorator(fun): sig=s ...
- vue - for遍历数组
注释上,也很清楚了哈. 1. item是循环名字,items是循环的数组 <!DOCTYPE html> <html lang="en"> <head ...
- windows下流媒体nginx-rmtp-module服务器搭建及java程序调用fmpeg将rtsp转rtmp直播流【转】
https://github.com/illuspas/nginx-rtmp-win32 http://bashell.sinaapp.com/archives/build-nginx-rtmp-mo ...
- E492: Not an editor command: ^M
在windows下拷贝vimrc到Linux,运行vim命令后,出现错误 vim E492: Not an editor command: ^M 原因: linux的文件换行符为\n,但windows ...
- 04-spring-控制反转
使用myeclipse开发spring一个Demo. 第一步:新建一个web project. 第二步:安装spring开发的支持包. 安装后多了这几个东西 3,定义一个操作接口: package c ...