詳解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;
}
}
子獲得父實(shí)例
如果不了解forwardRef用處的的可以看 #11
@Host 表示這個(gè)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;
}
父獲得子實(shí)例
子元素指令在父constructor時(shí)是獲取不到的,所以必須在組件的ngAfterViewInit生命周期鉤子后才能獲取,如果對(duì)組件生命周期不了解的話,可以參考#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)
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
AngularJS實(shí)現(xiàn)表單驗(yàn)證
客戶端表單驗(yàn)證是AngularJS里面最酷的功能之一。 AngularJS表單驗(yàn)證可以讓你從一開始就寫出一個(gè)具有交互性和可相應(yīng)的現(xiàn)代HTML5表單。在AngularJS中,有許多表單驗(yàn)證指令。在這里,我們將談?wù)剮讉€(gè)最流行指令,然后我們將討論如何編寫自定義的驗(yàn)證。2015-01-01
利用Angular.js限制textarea輸入的字?jǐn)?shù)
相信在大家已經(jīng)學(xué)習(xí)了足夠多關(guān)于AngularJS的知識(shí)后,就可以開始創(chuàng)建第一個(gè)AngularJS應(yīng)用程序,這篇文章通過(guò)示例給大家介紹如何利用Angular.js限制textarea輸入的字?jǐn)?shù),有需要的朋友們可以參考借鑒,下面來(lái)一起看看吧。2016-10-10
解決ng-repeat產(chǎn)生的ng-model中取不到值的問(wèn)題
今天小編就為大家分享一篇解決ng-repeat產(chǎn)生的ng-model中取不到值的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
詳解使用angularjs的ng-options時(shí)如何設(shè)置默認(rèn)值(初始值)
本篇文章主要介紹了詳解使用angularjs的ng-options時(shí)如何設(shè)置默認(rèn)值(初始值),具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
AngularJS中關(guān)于ng-class指令的幾種實(shí)現(xiàn)方式詳解
這篇文章給大家介紹了angularJS中ng-class指令的三種實(shí)現(xiàn)方式,其中包括通過(guò)數(shù)據(jù)的雙向綁定、通過(guò)對(duì)象數(shù)組和通過(guò)key/value這三種方式,有需要的朋友們可以參考學(xué)習(xí),下面來(lái)一起看看吧。2016-09-09
angular2+node.js express打包部署的實(shí)戰(zhàn)
本篇文章主要介紹了angular2+node.js express打包部署的實(shí)戰(zhàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
AngularJS學(xué)習(xí)第一篇 AngularJS基礎(chǔ)知識(shí)
這篇文章主要介紹了AngularJS學(xué)習(xí)第一篇,分享了有關(guān)AngularJS的基礎(chǔ)知識(shí),主要包括指令、過(guò)濾器等,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
angularJS 發(fā)起$http.post和$http.get請(qǐng)求的實(shí)現(xiàn)方法
本篇文章主要介紹了angularJS 發(fā)起$http.post和$http.get請(qǐng)求的實(shí)現(xiàn)方法,分別介紹了$http.post和$http.get請(qǐng)求的方法,有興趣的可以了解一下2017-05-05

