我有一个自定义的 Angular 输入组件,它使用 textarea
作为输入字段。
如何将回车键事件传播到父窗体?
最佳答案
您可以创建一个自定义事件,一旦收到输入键事件,该事件就会发送给父级。
在您的自定义输入元素中:
@Output() onEnterPressed:EventEmitter<any> = new EventEmitter<any>();
// Method called on KeyUp event. Using KeyUp as it recommended by Angular for Keyboard events.
handleKeyPressed(keyEvent:KeyboardEvent) {
if(enter_key_is_pressed) {
this.onEnterPressed.emit(keyEvent);
}
}
在父组件模板中:
<custom-input [onEnterPressed]="handleEnterEvent($event)"></custom-input>
在父组件组件类中:
handleEnterEvent(event:KeyboardEvent):void {
//do_whatever_you_want
}
关于javascript - 在自定义 Angular 输入中传播输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47621389/