我目前正在开发一个Angular 2应用程序,我正在尝试在子组件中调用一个函数,但似乎没有任何地方.
我的父组件如下所示:
app.component.ts
@Component({
selector: 'piano-app',
styleUrls: ['app/components/app/app.component.css'],
template: `
<div id="gameWrapper">
<note-canvas [keyPressed]="pressed"></note-canvas>
<piano (key-pressed)="keyPressed($event)"></piano>
</div>
`,
directives: [PianoComponent, NoteCanvasComponent],
})
export class AppComponent {
public pressed: any;
// This event is successfully called from PianoComponent
keyPressed(noteData) {
console.log(noteData); // {key: 30, keyType: "white"}
this.pressed = noteData;
}
}
Run Code Online (Sandbox Code Playgroud)
和子组件看起来像这样:
注意,canvas.component.ts
export class NoteCanvasComponent implements OnInit {
...
@Input() keyPressed : any;
constructor(private element: ElementRef, private noteGenerator: NoteFactory) {
this.canvas = this.element.nativeElement.querySelector('canvas');
this.context = this.canvas.getContext('2d');
this.canvasWidth = 900; …Run Code Online (Sandbox Code Playgroud) angular ×1