简体   繁体   English

如何从Angular 2中的子组件事件触发父组件中的本地引用?

[英]How to trigger local reference in parent component from child component event in Angular 2?

I am trying to work out how to trigger a local reference in a parent component template ie #rightNav from a child component template click event (click)="rightNav.open()" using Material 2 sidenav . 我正在尝试找出如何使用材料2 sidenav在子组件模板点击事件(click)="rightNav.open()"触发父组件模板(即#rightNav的本地引用。 I think I need to use the @ViewChild annotation, but not sure how. 我想我需要使用@ViewChild批注,但不确定如何使用。

Child component template (app-conditions-list): 子组件模板(app-conditions-list):

<div *ngFor="let condition of conditions" [class.selected]="condition === selectedCondition"
            (click)="rightNav.open()"></div>

Parent component template (condition component): 父组件模板(条件组件):

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"
}

The child component is nested in the parent component template. 子组件嵌套在父组件模板中。 Thanks! 谢谢!

You can add an output to the child component and listen to events from it 您可以将输出添加到子组件并从中监听事件

export class ConditionsListComponent {
  @Output() navOpen:EventEmitter = new EventEmitter();
}

You can use a template variable to refer to sibling elements like: 您可以使用模板变量来引用同级元素,例如:

<div #rightNav align="end" mode="side" (close)="close($event)"</div>
<app-conditions-list (navOpen)="rightNav.open()"></app-conditions-list>`,

and event an event like 并发生类似的事件

<div *ngFor="let condition of conditions" [class.selected]="condition === selectedCondition"
        (click)="navOpen.next(null)"></div>

You'd need to bubble your event up from your child to your parent : 您需要将事件从孩子传给父母:

The child :  

export class ConditionsListComponent  {
   @Output('myEvent') myEvent = new EventEmitter();

   private bubbleUp($event:Event){

     myEvent.emit($event)
  }
}

It's view : 视图:

  <div *ngFor="let condition of conditions" [class.selected]="condition === selectedCondition"
        (click)="bubbleUp($event)"></div>

And the 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()
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Angular 2中的父组件触发子组件中的本地引用 - How to trigger local reference in child component from parent component in Angular 2 父组件中的触发事件来自路由Angular中子组件的加载 - Trigger event in Parent component from child component load in routing Angular 如何从父组件OnSubmit Angular 4触发子组件的验证? - how to trigger a validation of child component from parent component OnSubmit Angular 4? 如何使用事件从父组件触发子 function - How do I trigger child function from parent component with event 如何从 angular 中的父组件触发子组件的 function? - How to trigger a function of child component from parent in angular? 如何在一个组件中触发通知事件并在Angular 4中侦听另一个组件(组件没有父/子关系) - How to trigger notification event in one component and listen in another component in Angular 4 (components do not have parent/child relation) Angular2,如何将事件从父组件广播到子指令? - Angular2,How to broadcast event from parent component to child directive? 如何在ngSwitch的Angular 2+中将事件从子级绑定到父级组件 - How to bind event from child to parent component in Angular 2+ in ngSwitch angular keyup事件不会在子组件上触发,而只会在父组件上触发 - angular keyup event does not trigger on child component, but only on parent instead 从子组件到父组件的Angular HostListener事件 - Angular HostListener event from Child Component to Parent Component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM