(一)父子组件 输入/输出属性

     关键词  Input,Output,EventEmitter。

   父子组件信息信息,分为

    (1)子组件向父组件传递

    (2)父组件向子组件传递

(二)模版变量与 @ViewChild

     (1)模版变量

     给子组件设定一个Id值,在HTML页面中,直接通过Id值,操作子组件的属性值。

import {Component, OnInit} from '@angular/core';
@Component({
selector: 'app-child-component',
template: `I'm {{ name }}`
})
export class ChildComponent implements OnInit {
public name: string;
constructor() {}
ngOnInit() {}
}

  父组件

import {Component, OnInit} from '@angular/core';
@Component({
selector: 'app-parent-component',
template: `
<app-child-component #child></app-child-component>
<br>
<button (click)="child.name = childName">设置子组件名称</button>
`
}) export class ParentComponent implements OnInit {
private childName: string;
constructor() { }
ngOnInit() {
this.childName = 'jack child';
}
}

  (2) @ViewChild

     与模版变量类似,在ts代码里操作子组件的属性值。

    子组件

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

@Component({
selector: 'app-child-component',
template: `I'm {{ name }}`
})
export class ChildComponent implements OnInit {
public name: string;
constructor() { }
ngOnInit() { }
}

    父组件

import {Component, OnInit, ViewChild} from '@angular/core';
import { ChildComponent} from './t11';
@Component({
selector: 'app-parent-component',
template: `
<app-child-component #child></app-child-component>
<br>
<button (click)="setChildName()">设置子组件名称</button>
`
}) export class ParentComponent implements OnInit {
@ViewChild(ChildComponent)
childCmp: ChildComponent;
constructor() { }
ngOnInit() { }
setChildName() {
this.childCmp.name = 'give it jack child';
}
}

(三)RxJS Subject

   使用关键词  Observable,Subject,Subscription。

   这种方式通过一个 Service 实例作为中央事件总线,用它来触发事件和监听事件,从而实现任何组件间的通信,包括 父子,兄弟,跨级。

import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable()
export class MessageService {
subject = new Subject<any>();
getMessage() {
return this.subject.asObservable();
} sendMessage(msg){
this.subject.next(msg);
} clearMessage() {
this.subject.next();
}
}

子组件

import { Component, OnInit } from '@angular/core';
import { MessageService } from '../service/message.service';
@Component({
selector: 'app-child',
template: `
<div>
<button (click)="sendMessage()">Send Message</button>
<button (click)="clearMessage()">Clear Message</button>
</div>`
})
export class ChildComponent implements OnInit {
constructor(private messageService: MessageService) {
} ngOnInit() {
}
sendMessage(): void {
this.messageService.sendMessage('hello from child ');
} clearMessage(): void {
this.messageService.clearMessage();
}
}

父组件

import { Component, OnInit } from '@angular/core';
import { MessageService } from '../service/message.service';
import { Subscription } from 'rxjs'; @Component({
selector: 'app-parent',
template: `
<div>
<div *ngIf="message">{{message}}</div>
<app-child></app-child>
</div>
`,
providers: [ MessageService]
}) export class ParentTestComponent implements OnInit {
message;
subscription = new Subscription();
constructor(private messageService: MessageService) {
this.subscription = this.messageService.getMessage().subscribe( res => {
this.message = res;
});
}
ngOnInit() {
this.subscription.unsubscribe();
}
}

 总结:

   从继承关系来看,

    1、 class EventEmitter<T> extends Subject<T> 。

    2、class Subject<T> extends Observable<T> implements SubscriptionLike

    Subscription implements SubscriptionLike

    4、interface SubscriptionLike extends Unsubscribable

   

Angular 组件通讯方式的更多相关文章

  1. vue 组件通讯方式到底有多少种 ?

    前置 做大小 vue 项目都离不开组件通讯, 自己也收藏了很多关于 vue 组件通讯的文章. 今天自己全部试了试, 并查了文档, 在这里总结一下并全部列出, 都是简单的例子. 如有错误欢迎指正. 温馨 ...

  2. Angular1.x组件通讯方式总结

    Angular1开发模式 这里需要将Angular1分为Angular1.5之前和Angular1.5两个不同的阶段来讲,两者虽然同属Angular1,但是在开发模式上还是有较大区别的.在Angula ...

  3. Angular1组件通讯方式总结

    这里需要将Angular1分为Angular1.5之前和Angular1.5两个不同的阶段来讲,两者虽然同属Angular1,但是在开发模式上还是有较大区别的.在Angular1.4及以前,主要是基于 ...

  4. angular,vue,react的基本语法—动态属性、事件绑定、ref,angular组件创建方式

    基本语法: 动态属性: vue: v-bind:attr="msg" :attr="msg" react: attr={msg} angular [attr]= ...

  5. Angular 组件通讯、生命周期钩子 小结

    本文为原创,转载请注明出处: cnzt       文章:cnzt-p http://www.cnblogs.com/zt-blog/p/7986858.html http://www.cnblogs ...

  6. vue组件通讯之provide / inject

    什么是 provide / inject [传送门] vue的组件通讯方式我们熟知的有 props $emit bus vuex ,另外就是 provide/inject provide/inject ...

  7. Angular组件——父子组件通讯

    Angular组件间通讯 组件树,1号是根组件AppComponent. 组件之间松耦合,组件之间知道的越少越好. 组件4里面点击按钮,触发组件5的初始化逻辑. 传统做法:在按钮4的点击事件里调用组件 ...

  8. angular组件之间的通讯

    组件通讯,意在不同的指令和组件之间共享信息.如何在两个多个组件之间共享信息呢. 最近在项目上,组件跟组件之间可能是父子关系,兄弟关系,爷孙关系都有.....我也找找了很多关于组件之间通讯的方法,不同的 ...

  9. Angular组件之间通讯

    组件之间会有下列3种关系: 1. 父子关系 2. 兄弟关系 3. 没有直接关系 通常采用下列方式处理(某些方式是框架特有)组件间的通讯,如下: 1父子组件之间的交互(@Input/@Output/模板 ...

随机推荐

  1. python补充之进制转换、exec、eval、compile

    目录 eval.exec和compile 1.eval函数 2.exec函数 eval()函数和exec()函数的区别 python中的进制转换 eval.exec和compile 1.eval函数 ...

  2. Spring源码构建

    1.下载spring源码并解压 https://codeload.github.com/spring-projects/spring-framework/zip/v5.0.2.RELEASE 打开bu ...

  3. 奇偶选择器:使用odd和even属性实现表格单双行颜色相间和不同

    一.奇偶选择器 表格在呈现数据的时候我们为了方便观看,而隔行显示不同的颜色,虽然可以用用类选择器 class实现这种效果,但是未免太过麻烦,为了更加简便地表现这种特殊的效果我们需要用到一种特殊的选择器 ...

  4. computed属性和watcher

    computed属性 在模板中使用表达式是非常方便直接的,然而这只适用于简单的操作.在模板中放入太多的逻辑,会使模板过度膨胀和难以维护.例如: <div id="example&quo ...

  5. C#删除文件夹的文件

    using System.IO; //判断文件是不是存在if(File.Exists(@"文件路径")){//如果存在则删除File.Delete(@"文件路径" ...

  6. Windows上安装Apache

    1.下载 (1)进入Apache官网http://httpd.apache.org— (2)点击Download (3)点击Files for Microsoft Windows (4)点击Apach ...

  7. Java——容器(Set)

    [Set接口] <1>Set接口是Collection的子接口,Set接口没有提供额外的方法. <2>实现Set接口的容器类中的元素是没有顺序的,而且不可以重复. <3& ...

  8. UOJ37. 【清华集训2014】主旋律

    http://uoj.ac/problem/37 题解 题目是让我们求出有多少个边集可以使这张图强连通. 先补集转化一下,求这张图不强连通的方案数. 我们考虑这样的图缩完点之后的情况,既然不强连通,那 ...

  9. hihocoder1286 : 子矩阵求和

    http://hihocoder.com/problemset/problem/1286 题解 NB分析题. 首先我们令\(s[i][j]\)表示以\((i,j)\)为左上角的矩形的权值和. 因为\( ...

  10. java读取ldif文件并创建新的节点

    所需jar包ldap.jar. jldap-4.3-source.jar http://www.java2s.com/Code/Jar/l/Downloadldapjar.htm 浏览器输入http: ...