简体   繁体   English

Angular 2:事件发射器

[英]Angular 2 : Eventemitter

I have a template like so: 我有一个像这样的模板:

<main>
    <footer></footer>
</main>

I want to emit a value from the footer to the main component. 我想从页脚向主要组件发出一个值。 On my parent I have a function called setStepCounter(value : number) : void . 在我的父母setStepCounter(value : number) : void我有一个名为setStepCounter(value : number) : void的函数。 When I add the eventemitter to my footer component like so: 当我将事件发射器添加到页脚组件中时,如下所示:

@Component({
    selector: 'footer',
    template: `<ng-content></ng-content> `,
    host: {
        '(stepCounterEmitter)': 'setStepCounter($event)'
    }
})

I get the following error: self.context.setStepCounter is not a function. 我收到以下错误:self.context.setStepCounter不是一个函数。

@Component({
    selector: 'footer',
    template: `<ng-content></ng-content> <button (click)="nextCounter()>click me</button> `,
})
export class Footer {
  @Output() stepCounter:EventEmitter<number> = new EventEmitter();

  private counter:number = null;
  nextCounter() {
    this.stepCounter.emit(this.counter.++);
  }
}
<main #main>
    <footer (stepCounter)="main.setStepCounter($event)"></footer>
</main>
export class MainComponent {
  setStepCounter(event:any) {
    ...
  }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM