詳解Angular2組件之間如何通信
組件之間的共享可以有好幾種方式
父->子 input 方式
import {Component,Input} from 'angular2/core'; @Component({ selector: 'child', template: ` <h2>child {{content}}</h2> ` }) class Child { @Input() content:string; } @Component({ selector: 'App', directives: [Child], template: ` <h1>App</h1> <child [content]="i"></child> ` }) export class App { i:number = 0; constructor() { setInterval(()=> { this.i++; }, 1000) } }
子->父 output 方式
import {Output,EventEmitter,Component} from 'angular2/core'; @Component({ selector: 'child', template: ` <h2>child</h2> ` }) class Child { @Output() updateNumberI:EventEmitter<number> = new EventEmitter(); i:number = 0; constructor() { setInterval(()=> { this.updateNumberI.emit(++this.i); }, 1000) } } @Component({ selector: 'App', directives: [Child], template: ` <h1>App {{i}}</h1> <child (updateNumberI)="numberIChange($event)"></child> ` }) export class App { i:number = 0; numberIChange(i:number){ this.i = i; } }
子獲得父實例
如果不了解forwardRef用處的的可以看 #11
@Host 表示這個Injector必須是host element在這里可以理解為 parent
import {Host,Component,forwardRef} from 'angular2/core'; @Component({ selector: 'child', template: ` <h2>child</h2> ` }) class Child { constructor(@Host() @Inject(forwardRef(()=> App)) app:App) { setInterval(()=> { app.i++; }, 1000); } } @Component({ selector: 'App', directives: [Child], template: ` <h1>App {{i}}</h1> <child></child> ` }) export class App { i:number = 0; }
父獲得子實例
子元素指令在父constructor時是獲取不到的,所以必須在組件的ngAfterViewInit生命周期鉤子后才能獲取,如果對組件生命周期不了解的話,可以參考#56
import {ViewChild,Component} from 'angular2/core'; @Component({ selector: 'child', template: ` <h2>child {{i}}</h2> ` }) class Child { i:number = 0; } @Component({ selector: 'App', directives: [Child], template: ` <h1>App {{i}}</h1> <child></child> ` }) export class App { @ViewChild(Child) child:Child; ngAfterViewInit() { setInterval(()=> { this.child.i++; }, 1000) } }
service 方式
import {Component,Injectable} from 'angular2/core'; @Injectable(); class KittencupService { i:number = 0; } @Component({ selector: 'child', template: ` <h2>child {{service.i}}</h2> ` }) class Child { constructor(public service:KittencupService){ } } @Component({ selector: 'App', directives: [Child], providers: [KittencupService], template: ` <h1>App {{i}}</h1> <child></child> ` }) export class App { constructor(service:KittencupService) { setInterval(()=> { service.i++; }, 1000) } }
service EventEmitter方式
import {Component,Injectable,EventEmitter} from 'angular2/core'; @Injectable() class KittencupService { change: EventEmitter<number>; constructor(){ this.change = new EventEmitter(); } } @Component({ selector: 'child', template: ` <h2>child {{i}}</h2> ` }) class Child { public i:number = 0; constructor(public service:KittencupService){ service.change.subscribe((value:number)=>{ this.i = value; }) } } @Component({ selector: 'App', directives: [Child], providers: [KittencupService], template: ` <h1>App {{i}}</h1> <child></child> ` }) export class App { i:number = 0; constructor(service:KittencupService) { setInterval(()=> { service.change.emit(++this.i); }, 1000) } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
利用Angular.js限制textarea輸入的字數(shù)
相信在大家已經(jīng)學習了足夠多關(guān)于AngularJS的知識后,就可以開始創(chuàng)建第一個AngularJS應(yīng)用程序,這篇文章通過示例給大家介紹如何利用Angular.js限制textarea輸入的字數(shù),有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-10-10解決ng-repeat產(chǎn)生的ng-model中取不到值的問題
今天小編就為大家分享一篇解決ng-repeat產(chǎn)生的ng-model中取不到值的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10詳解使用angularjs的ng-options時如何設(shè)置默認值(初始值)
本篇文章主要介紹了詳解使用angularjs的ng-options時如何設(shè)置默認值(初始值),具有一定的參考價值,有興趣的可以了解一下2017-07-07AngularJS中關(guān)于ng-class指令的幾種實現(xiàn)方式詳解
這篇文章給大家介紹了angularJS中ng-class指令的三種實現(xiàn)方式,其中包括通過數(shù)據(jù)的雙向綁定、通過對象數(shù)組和通過key/value這三種方式,有需要的朋友們可以參考學習,下面來一起看看吧。2016-09-09angular2+node.js express打包部署的實戰(zhàn)
本篇文章主要介紹了angular2+node.js express打包部署的實戰(zhàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07AngularJS學習第一篇 AngularJS基礎(chǔ)知識
這篇文章主要介紹了AngularJS學習第一篇,分享了有關(guān)AngularJS的基礎(chǔ)知識,主要包括指令、過濾器等,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02angularJS 發(fā)起$http.post和$http.get請求的實現(xiàn)方法
本篇文章主要介紹了angularJS 發(fā)起$http.post和$http.get請求的實現(xiàn)方法,分別介紹了$http.post和$http.get請求的方法,有興趣的可以了解一下2017-05-05