我在HTML模板中引用了同名的TemplateRef
!
<ng-template #eventEmitter>
对此抱歉,仍然保留引用错误代码的问题。
我试图在组件的模板something.component.html
中使用EventEmitter
实例的emit()
方法
<div (click)="eventEmitter.emit()">click me</div>
并在我的组件something.component.ts
中定义,如
@Output() eventEmitter = new EventEmitter<any>();
并收到以下错误
"jit_nodeValue_3(...).emit is not a function"
答案 0 :(得分:0)
在EventEmitter
上设置Output
:
import { Component, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'demo',
template: `<h1>Demo</h1>
<button (click)="notify.emit('hello')">Notify</button>`
})
export class DemoComponent {
@Output() notify = new EventEmitter<any>();
}
订阅活动:
import { Component } from '@angular/core';
@Component({
selector: 'app',
template: `<h1>App</h1>
<demo (notify)="receiveNotification($event)"></demo>`
})
export class AppComponent {
notifications = new Array<any>();
receiveNotification(notification: any) {
this.notifications.push(notification);
}
}