组件间通讯

(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学习笔记-组件间通讯的更多相关文章

  1. AngularJs学习笔记-组件生命周期

    组件生命周期 (1)组件生命周期钩子 constructor:组件创建时被创建 ngOnChanges: 父组件修改或初始化子组件的输入属性时被调用,如果子组件没有输入属性,则永远不会被调用,它的首次 ...

  2. Angular6 学习笔记——组件详解之组件通讯

    angular6.x系列的学习笔记记录,仍在不断完善中,学习地址: https://www.angular.cn/guide/template-syntax http://www.ngfans.net ...

  3. AngularJs学习笔记--directive

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/directive Directive是教HTML玩一些新把戏的途径.在DOM编译期间,directiv ...

  4. AngularJs学习笔记--concepts(概念)

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/concepts 继续.. 一.总括 本文主要是angular组件(components)的概览,并说明 ...

  5. AngularJs学习笔记--Creating Services

    原版地址:http://docs.angularjs.org/guide/dev_guide.services.creating_services 虽然angular提供许多有用的service,在一 ...

  6. AngularJs学习笔记--I18n/L10n

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/i18n 一.I18n and L10n in AngularJS 1. 什么是I18n和L10n? 国 ...

  7. AngularJs学习笔记--Scope

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/scope 一.什么是Scope? scope(http://code.angularjs.org/1. ...

  8. AngularJs学习笔记--Dependency Injection(DI,依赖注入)

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/di 一.Dependency Injection(依赖注入) 依赖注入(DI)是一个软件设计模式,处理 ...

  9. AngularJs学习笔记--Understanding the Controller Component

    原版地址:http://docs.angularjs.org/guide/dev_guide.mvc.understanding_model 在angular中,controller是一个javasc ...

随机推荐

  1. kuangbin专题十二 HDU1029 Ignatius and the Princess IV (水题)

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

  2. Python网络编程(一)

    最近在啃<python核心编程(第三版)>,感觉这本书并不是特别的友好,虽然有基于python3提出的改进代码:但是整书的基准感觉还是在python2.7.所以python3的代码中还是有 ...

  3. mysql事务锁表

    -- 查看被锁住的SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS; -- 等待锁定SELECT * FROM INFORMATION_SCHEMA.INNO ...

  4. python模块之calendar方法详细介绍

    calendar,是与日历相关的模块.calendar模块文件里定义了很多类型,主要有Calendar,TextCalendar以及HTMLCalendar类型.其中,Calendar是TextCal ...

  5. 关于Spring @RequestBody 自动映射模型原理

    关于Spring @RequestBody 自动映射模型 2016年10月18日 22:17:12 稻子丶 阅读数:5049   在很多时候,Spring的注解为我们提供了很多方便,但只知道其用法,不 ...

  6. 文件系统满的话(filesystem full),该如何处理。(du and ls)

    #!/bin/bash function ergodic(){ ` do "/"$file ] then ergodic $"/"$file else loca ...

  7. LeetCode 069 Sqrt(x) 求平方根

    Implement int sqrt(int x).Compute and return the square root of x.x is guaranteed to be a non-negati ...

  8. vim 编辑器设置tab缩进

    创建 ~/.vimrc文件,写入 set tabstop=4 ,保存 原文

  9. Java文件与io——字节数组流数据流字符串流

    字节数组流 ByteArrayInputStream:包含一个内部缓冲区,该缓冲区包含从流中读取的字节.内部计数器跟踪read方法要提供的下一个字节.关闭ByteArrayInputStream无效. ...

  10. HBase 相关API操练(三):MapReduce操作HBase

    MapReduce 操作 HBase 在 HBase 系统上运行批处理运算,最方便和实用的模型依然是 MapReduce,如下图所示. HBase Table 和 Region 的关系类似 HDFS ...