angular keyup事件不会在子组件上触发,而只会在父组件上触发

公顷

我创建了一个组件,希望在我的应用程序中拥有多个版本,因为我想将所有逻辑都保留在一个地方,所以我创建了一个ParentComponent,将我的所有代码都放在其中。

但是angular不继承组件注释,因此我还创建了一个“ TemplateComponent”,以防止代码重复。TemplateComponent保存输入,但是由ParentComponent处理输入中的事件。这样,每个ChildComponent都继承逻辑的ParentComponent,并且它的模板是TemplateComponent的实例。

最重要的是,当我尝试在TemplateComponent内的输入上侦听keyup事件时,在没有任何输入或控件的ChildComponent上调用了click事件。

我试图将事件绑定传递给模板组件以连接键盘输入,但它从未触发。

ChildComponent上有一个HostListener,目的是调试事件仅在此处触发。这并不是导致事件在此停止的原因。

有没有一种方法可以告诉angular必须在模板组件内部调用keydown事件,或者是将所有内容链接在一起的另一种方法?

我有一个工作的小伙子,这很不言而喻:

https://plnkr.co/edit/e4U6DB4d47ULpLppHd4H?p=preview

而且还有代码:

ParentComponent:

import {Component, NgModule, VERSION} from '@angular/core'

@Component({
  selector: 'app-parent',
  template: `
    <ng-content></ng-content>
  `,
})
export class ParentComponent {

  printText = 'Not yet';

  constructor() {
  }

  inputKeyDownEnter(event: any) {
    this.printText = 'KeyDown OK!';
  }

  clearButtonClick(event: any) {
    this.printText = "Clear button click!";
  }
}

ChildComponent:

import {Component, HostListener} from '@angular/core';
import {ParentComponent} from './parent.component';

@Component({
  selector: 'app-child',
  template: `
    <div>{{printText}}</div>
    <app-custom-template
      (inputKeyDownEnter)="inputKeyDownEnter"
      (clearButtonClick)="clearButtonClick"
    >
    </app-custom-template>
  `,
})
export class ChildComponent extends ParentComponent {

  constructor() {
    super();
  }

  @HostListener('keydown.enter', ['$event'])
  onKeyDownChild(event: any) {
    console.log('Only here :( !');
  }

}

TemplateComponent:

import {Component, Output, EventEmitter} from '@angular/core';

@Component({
  selector: 'app-custom-template',
  template: `
    <input 
      type="text"
      name="my_text"
      (keyup.Enter)="inputKeyDownEnter.emit($event)"
      />
    <button
      type="button"
      (click)="clearButtonClick.emit($event)">Clear</button>
  `,
})
export class CustomTemplateComponent {

  @Output()
  inputKeyDownEnter = new EventEmitter<any>();

  @Output()
  clearButtonClick = new EventEmitter<any>();

  constructor() {
  }

}
yurzui

你应该调用方法

(inputKeyDownEnter)="inputKeyDownEnter($event)"
                                      ^^^^^^^^^

柱塞

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

angular2子组件不会在从父组件触发的后续事件上加载

PrimeNG + Angular 4:组件中设置的切换按钮不会在屏幕上更新

Angular 4 导航方法不会在目标组件(ngIf、ngFor)上呈现页面内容

Angular onChange不会在数据更改时触发

(swipeup)不会在Hammerjs的Angular 4中触发

Angular MatTabChangeEvent不会在页面加载时触发

反应路由呈现的组件不会在父级上触发 setState

组件不会在 Angular 9 的子路由中呈现

如何从Angular 2中的子组件事件触发父组件中的本地引用?

音频事件不会在播放事件上触发jquery

触发子组件angular的ngOnInit

父控制器上的$ scope。$ watch不会在子ng-select上触发

Angular 2动态加载的组件,事件不会触发吗?

不会在TextInputEditText android上触发onClick事件

OnPageIndexChanging事件永远不会在GridView上触发

Firestore函数不会在事件上触发

CSS:子元素不会在父元素上触发悬停

服务不会在 Angular 4.x 中触发发布请求

OnLongClickListener不会在ViewPager上触发

在Angular2中,ngModel值不会在自定义指令的onBlur事件上更新

自定义Angular组件不会在模板中呈现为TR

Angular 组件属性不会在引用属性更改时更新

为什么 angular 不会在动态创建的组件中填充视图?

Angular 自定义手风琴组件不会在单击时展开

<div> 上的点击事件不会在 Firefox 上触发,但在 Chrome 上触发

Angular2 EventEmitter触发父组件

如何从Angular 2的父组件中触发子组件中的本地引用

React不会在组件中触发功能

ngrx订阅的存储不会在状态更改Angular5上更新