Angular2中子组件事件如何触发父组件中的局部引用?
How to trigger local reference in parent component from child component event in Angular 2?
我正在尝试找出如何在父组件模板中触发本地引用,即使用 Material 2 sidenav 从子组件模板点击事件 (click)="rightNav.open()"
#rightNav
。我想我需要使用 @ViewChild
注释,但不确定如何使用。
子组件模板(app-conditions-list):
<div *ngFor="let condition of conditions" [class.selected]="condition === selectedCondition"
(click)="rightNav.open()"></div>
父组件模板(条件组件):
import { Component} from '@angular/core';
import { ConditionsListComponent } from './listComponent/conditions-list.component';
@Component({
moduleId: module.id,
selector: 'app-conditions',
template: `
<md-sidenav #rightNav align="end" mode="side">
"Condition details will open here on click event"
</md-sidenav>
<app-conditions-list></app-conditions-list>`,
styleUrls: ['./conditions.component.css'],
directives: [
ConditionsListComponent,
]
})
export class ConditionsComponent {
title = "Conditions Manager"
}
子组件嵌套在父组件模板中。
谢谢!
您可以向子组件添加一个输出并监听它的事件
export class ConditionsListComponent {
@Output() navOpen:EventEmitter = new EventEmitter();
}
您可以使用模板变量来引用同级元素,例如:
<div #rightNav align="end" mode="side" (close)="close($event)"</div>
<app-conditions-list (navOpen)="rightNav.open()"></app-conditions-list>`,
并举办类似
的活动
<div *ngFor="let condition of conditions" [class.selected]="condition === selectedCondition"
(click)="navOpen.next(null)"></div>
您需要将活动从 child 提升到 parent:
The child :
export class ConditionsListComponent {
@Output('myEvent') myEvent = new EventEmitter();
private bubbleUp($event:Event){
myEvent.emit($event)
}
}
它的观点:
<div *ngFor="let condition of conditions" [class.selected]="condition === selectedCondition"
(click)="bubbleUp($event)"></div>
和parent:
import { Component} from '@angular/core';
@Component({
moduleId: module.id,
selector: 'app-conditions',
template: `
<div #rightNav align="end" mode="side" (close)="close($event)"</div>
<app-conditions-list (myEvent)='gotTheEvent($event)' ></app-conditions-list>`,
styleUrls: ['./conditions.component.css'],
providers: [],
directives: [
ConditionsListComponent,
]
})
export class ConditionsComponent {
title = "Conditions Manager";
gotTheEvent($event){
console.log('I got this event from my child',$event);
//you can do whatever you want :
rightNav.open()
}
}
我正在尝试找出如何在父组件模板中触发本地引用,即使用 Material 2 sidenav 从子组件模板点击事件 (click)="rightNav.open()"
#rightNav
。我想我需要使用 @ViewChild
注释,但不确定如何使用。
子组件模板(app-conditions-list):
<div *ngFor="let condition of conditions" [class.selected]="condition === selectedCondition"
(click)="rightNav.open()"></div>
父组件模板(条件组件):
import { Component} from '@angular/core';
import { ConditionsListComponent } from './listComponent/conditions-list.component';
@Component({
moduleId: module.id,
selector: 'app-conditions',
template: `
<md-sidenav #rightNav align="end" mode="side">
"Condition details will open here on click event"
</md-sidenav>
<app-conditions-list></app-conditions-list>`,
styleUrls: ['./conditions.component.css'],
directives: [
ConditionsListComponent,
]
})
export class ConditionsComponent {
title = "Conditions Manager"
}
子组件嵌套在父组件模板中。 谢谢!
您可以向子组件添加一个输出并监听它的事件
export class ConditionsListComponent {
@Output() navOpen:EventEmitter = new EventEmitter();
}
您可以使用模板变量来引用同级元素,例如:
<div #rightNav align="end" mode="side" (close)="close($event)"</div>
<app-conditions-list (navOpen)="rightNav.open()"></app-conditions-list>`,
并举办类似
的活动<div *ngFor="let condition of conditions" [class.selected]="condition === selectedCondition"
(click)="navOpen.next(null)"></div>
您需要将活动从 child 提升到 parent:
The child :
export class ConditionsListComponent {
@Output('myEvent') myEvent = new EventEmitter();
private bubbleUp($event:Event){
myEvent.emit($event)
}
}
它的观点:
<div *ngFor="let condition of conditions" [class.selected]="condition === selectedCondition"
(click)="bubbleUp($event)"></div>
和parent:
import { Component} from '@angular/core';
@Component({
moduleId: module.id,
selector: 'app-conditions',
template: `
<div #rightNav align="end" mode="side" (close)="close($event)"</div>
<app-conditions-list (myEvent)='gotTheEvent($event)' ></app-conditions-list>`,
styleUrls: ['./conditions.component.css'],
providers: [],
directives: [
ConditionsListComponent,
]
})
export class ConditionsComponent {
title = "Conditions Manager";
gotTheEvent($event){
console.log('I got this event from my child',$event);
//you can do whatever you want :
rightNav.open()
}
}