簡體   English   中英

Angular 2:事件發射器

[英]Angular 2 : Eventemitter

我有一個像這樣的模板:

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

我想從頁腳向主要組件發出一個值。 在我的父母setStepCounter(value : number) : void我有一個名為setStepCounter(value : number) : void的函數。 當我將事件發射器添加到頁腳組件中時,如下所示:

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

我收到以下錯誤: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