组件间通讯

(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. Python中list作为默认参数的陷阱

    在Python中,作为默认参数的一定要是不可变对象,如果是可变对象,就会出现问题,稍不注意,就会调入陷阱,尤其是初学者,比如我(┬_┬). 我们来看一个例子. def add(L=[]): L.app ...

  2. 利用canvas实现倒计时功能

    wxml代码:<view class=“page-body”><view class=“page-body-wrapper”><canvas canvas-id=“can ...

  3. webpack前端自动化构建工具

    博主不易,不求赞赏,希望把自己遇到的难点写出来,以及希望自己能有能力写出一篇不错的博文. 前端构建工具本人 bootstrap+jquery用gulp vue+element 用webpack 本人最 ...

  4. 判断当前用户是否在某个SharePoint组内

    /// <summary> /// 判断当前登录人是否在sharepoint组中 /// </summary> /// <param name="current ...

  5. python_魔法方法(六):迭代器和生成器

    迭代器 自始至终,都有一个概念一直在用,但是我们却没来都没有人在的深入剖析它.这个概念就是迭代. 迭代的意思有点类似循环,每一次的重复的过程被称为迭代的过程,而每一次迭代得到的结果会被用来作为下一次迭 ...

  6. upper_bound和lower_bound的用法

    首先介绍这两种函数是什么意思 upper_bound是找到大于t的最小地址,如果没有就指向末尾 lower_bound是找到大于等于t的最小地址 题目链接:https://vjudge.net/con ...

  7. 最大利润-城市A和B

    1,问题描述 jack每天同时只能在A和B其中一个城市工作赚钱,假设两个城市间的交通费为m.已知每天在A 和 B 能赚到多少钱,那么jack怎么选择每天工作的城市才能赚到最大利润. 比如 moneyA ...

  8. superset 配置连接 hbase

    1. 简单说明 最近配置superset查询hbase, 根据网上查询到的文档和经验,成功了一次(python3.4  superset 0.20.),后边重试换各种版本就不行了.最后根据错误终于发现 ...

  9. 牛客网Java刷题知识点之匿名对象

    不多说,直接上干货! 匿名对象的两种用途: 1.当对象对方法仅进行一次调用的时候,就可以简化成匿名对象. 2.匿名对象可以作为实际参数进行传递. 匿名对象顾名思义就是没有名字的对象. new Car( ...

  10. SpringMVC03 ParameterMethodNameResolver(参数方法名称解析器) And XmlViewResolver(视图解析器)

    参数方法名称解析器 1.配置依赖包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht ...