AngularJs学习笔记-组件间通讯
组件间通讯
(1)输入属性@Input

Tips:子组件属性的改变不会影响到父组件
如下,子组件中stockCode属性发生变化不会引起父组件stock属性的变化

(2)输入属性@Output
子组件tsimport { Component, OnInit, Output } from '@angular/core';
import { EventEmitter } from '@angular/core';
const STOCK_CODE = "IBM";
@Component({
selector: 'app-price-quote-inner',
templateUrl: './price-quote-inner.component.html',
styleUrls: ['./price-quote-inner.component.css']
})
export class PriceQuoteInnerComponent implements OnInit {
price: number = 0;
//在子组件的控制器中定义了EventEmitter属性,并为该属性添加@Output装饰器
//EventEmitter能够向外部组件发送信息,即触发外部组件的一个事件,外部组件可以监听该事件,@Output中的内容表示事件的名称
@Output("priceChange")
lastPrice: EventEmitter<PriceQuote> = new EventEmitter<PriceQuote>();
constructor() { }
ngOnInit() {
//每个1秒钟改变1次价格,并向外部发送该价格信息
//这里向外部发送了一个PriceQuote类型的变量
setInterval(() => {
const price = Math.random() * 10;
this.price = price;
this.lastPrice.emit(new PriceQuote(STOCK_CODE, price));
}, 1000);
}
}
export class PriceQuote {
constructor(public stockCode: string,
public price: number) {};
}
父组件ts
import { Component, OnInit } from '@angular/core';
import { PriceQuote } from '../price-quote-inner/price-quote-inner.component';
@Component({
selector: 'app-price-quote-outer',
templateUrl: './price-quote-outer.component.html',
styleUrls: ['./price-quote-outer.component.css']
})
export class PriceQuoteOuterComponent implements OnInit {
price: number = 0;
constructor() { }
ngOnInit() {
}
//监听子组件发送信息的方法
//传入的参数event即为子组件发送的内容
priceChangeHandler(event: PriceQuote) {
this.price = event.price;
}
}
父组件HTML
<!-- 监听通过在子组件中定义好的事件名称priceChange,通过priceChangeHandler处理事件的内容 -->
<app-price-quote-inner (priceChange)="priceChangeHandler($event)"></app-price-quote-inner>
<p>
Parent component: current priceQuote is {{price | number:"1.2-2"}}
</p>
效果如下

(3)中间人模式
中间人模式是一种设计模式,用于解耦组件间的通讯,可以通过父组件可service等方式对多个组件间的通讯进行解耦
当统一个父组件中的两个子组件需要进行通讯时,可以通过父组件监听发送方子组件发送的消息并把消息传递给接收方子组件的方式对两个子组件进行解耦。此时两个子组件都可以不需要知道彼此的存在,提高组件间的可重用性。


(4)父组件既可向子组件传递数据,也可调用子组件的api
1、在模板中调用

2、在控制器中调用

AngularJs学习笔记-组件间通讯的更多相关文章
- AngularJs学习笔记-组件生命周期
组件生命周期 (1)组件生命周期钩子 constructor:组件创建时被创建 ngOnChanges: 父组件修改或初始化子组件的输入属性时被调用,如果子组件没有输入属性,则永远不会被调用,它的首次 ...
- Angular6 学习笔记——组件详解之组件通讯
angular6.x系列的学习笔记记录,仍在不断完善中,学习地址: https://www.angular.cn/guide/template-syntax http://www.ngfans.net ...
- AngularJs学习笔记--directive
原版地址:http://code.angularjs.org/1.0.2/docs/guide/directive Directive是教HTML玩一些新把戏的途径.在DOM编译期间,directiv ...
- AngularJs学习笔记--concepts(概念)
原版地址:http://code.angularjs.org/1.0.2/docs/guide/concepts 继续.. 一.总括 本文主要是angular组件(components)的概览,并说明 ...
- AngularJs学习笔记--Creating Services
原版地址:http://docs.angularjs.org/guide/dev_guide.services.creating_services 虽然angular提供许多有用的service,在一 ...
- AngularJs学习笔记--I18n/L10n
原版地址:http://code.angularjs.org/1.0.2/docs/guide/i18n 一.I18n and L10n in AngularJS 1. 什么是I18n和L10n? 国 ...
- AngularJs学习笔记--Scope
原版地址:http://code.angularjs.org/1.0.2/docs/guide/scope 一.什么是Scope? scope(http://code.angularjs.org/1. ...
- AngularJs学习笔记--Dependency Injection(DI,依赖注入)
原版地址:http://code.angularjs.org/1.0.2/docs/guide/di 一.Dependency Injection(依赖注入) 依赖注入(DI)是一个软件设计模式,处理 ...
- AngularJs学习笔记--Understanding the Controller Component
原版地址:http://docs.angularjs.org/guide/dev_guide.mvc.understanding_model 在angular中,controller是一个javasc ...
随机推荐
- puppet批量管理500多台服务器
前言 puppet使用了有一段时间了,之前写的手顺书一直未发布到blog上来,今天正好有空,写下一点笔记.公司在用的服务器有500多台,基本都为CentOS,版本有5和6两种,管理起来很不方便,尤其是 ...
- 1、SpringMVC架构
1.SpringMVC架构 1.1 Spring web mvc 介绍 spring web mvc和Struts2都属于表现层的框架,它是Spring框架的一部分,我们可以从Spring的整体结构中 ...
- 分层图最短路【bzoj2763】: [JLOI2011]飞行路线
bzoj2763: [JLOI2011]飞行路线 Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0 ...
- shell脚本——循环和函数
1.打印一个等腰三角形 ` ;do -$i]` ;do echo -n ' ' done -]` ;do echo -n '*' done echo done 2.打印99乘法表 #!/bin/bas ...
- Groovy xlsx
如果在JMeter安装的“bin”文件夹下有Excel(xlsx)文件,则test.xlsx可以使用以下方法动态填充请求参数: 将tika-app.jar添加到JMeter Classpath 重新启 ...
- vue中比较完美请求的栗子(使用 axios 访问 API)
vue中比较完美请求的栗子(使用 axios 访问 API) 官网地址:https://vuejs.bootcss.com/v2/cookbook/using-axios-to-consume-api ...
- 分别使用http,express,koa请求第三方接口
今天又再次恶补了一下http的内容,确切地说是node.js里面的http的内容,啊,百度了半天express怎么请求第三方接口,结果发现自己买的入门书籍都有这个内容.舍近求远,我真是醉了.还有百度上 ...
- docker jvm 占用高的问题定位
定位流程 先使用一些轻便的工具查看总体情况, 如果情况糟糕, 再使用重量级的工具 jstack 查看线程数是否过多 jstat -gc -gcutil 查看gc次数和时间是否过多, 各个分 ...
- 安装pywin32出现--Python version 3.x required, which was not found in the registry
这两天安装pywin32时出现了这个问题 双击.exe文件进入安装界面,然后点击下一步,它会自动定位你的python安装在什么地方,但是我的安装过程中未自动定位到python安装位置,并显示显示: 安 ...
- 转 MYSQL_GTID详解
http://blog.itpub.net/27067062/viewspace-2141906/ 一.GTID概述 GTID是MYSQL5.6新增的特性,GTID(Global Transac ...