简体   繁体   English

如何在不使用模板的情况下在Angular 2中检测自定义事件?

[英]How do I detect a custom event in Angular 2 without using a template?

I have an Angular 2 project where I am dynamically generating child components and I want to listen to a custom event that is dispatched by that component. 我有一个Angular 2项目,我在其中动态生成子组件,并且我想听由该组件调度的自定义事件。

This is my parent component that generates the component and listens for events 这是我的父组件,它生成该组件并侦听事件

 var cmpRef: ComponentRef<any>;
let factory = this.componentFactoryResolver.resolveComponentFactory(type);
cmpRef = this.target.createComponent(factory);

// PROPERLY DETECTS EVENT 
cmpRef.instance.self.nativeElement.addEventListener('click',this.onEventFired);

// DOESN'T DETECT EVENT
cmpRef.instance.self.nativeElement.addEventListener('isLoaded',this.onEventFired);

// call this method to fire the event
(cmpRef.instance as ChildComponent).load();

And this is my ChildComponent 这是我的ChildComponent

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

@Component({
  selector: 'app-child',
  template: '  <p>child component- click on text to fire event!</p>'

})
export class ChildComponent implements OnInit, AfterViewInit {


  @Output('isLoaded') isLoaded:EventEmitter<boolean> = new EventEmitter<boolean>();

  constructor(public self:ElementRef)
  {
    console.log('ChildComponent Created')
  }

  public load()
  {
    console.log('load method called.... dispatching event')
     this.isLoaded.next(true);
  }
}

I need to be able to listen to the events that are being dispatched but for my purposes, I cannot use the template and want to listen directly from my typescript. 我需要能够侦听正在调度的事件,但是出于我的目的,我无法使用模板,而是希望直接从我的打字稿中侦听。 I have successfully done this with a "click" event but my custom events get dispatched but never gets detected. 我已经通过“ click”事件成功完成了此操作,但是我的自定义事件被调度了,但从未被检测到。

I have attached a plunker to show my use case 我已经附上了一个插件来展示我的用例

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

That's not how it's supposed to work, if you want to subscribe to your isLoaded event just cast that component created by the factory and subscribe to isLoaded event. 如果您要订阅isLoaded事件,只需将工厂创建的组件isLoaded并订阅isLoaded事件,这不是应该的工作方式。

Just change your: 只需更改:

// DOESN'T DETECT EVENT
cmpRef.instance.self.nativeElement.addEventListener('isLoaded',this.onEventFired);

For: 对于:

// DOESN'T DETECT EVENT
(<ChildComponent>cmpRef.instance).isLoaded.subscribe(() => {
  console.log("Has been loaded");
});

First case worked because that is a native event of the host. 第一种情况有效,因为这是主机的本地事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有触发事件的情况下从Angular模板调用函数? - How do I call a function from my Angular template without an event being fired? Angular 2 - 如何在没有模板的情况下收听事件? - Angular 2 - How to listen to event without a template? 模板未捕获Angular 2自定义事件 - Angular 2 custom event not caught by template 如何在Angular 5中实现多级组件/模板事件处理? - How do I achieve multi level component/template event handling in Angular 5? 如何使用 AST 从 angular html 模板中检测和提取变量? - How to detect and extract variables from angular html template using AST? 如何在不通过模板连接的情况下监听ContentChild事件? - How do I listen to a ContentChild event without hooking up via template? 在 angular 中,如何在不使用导入的情况下在 angular 模板中使用管道? - In angular, how can I use pipe in angular template without using import? 在 Angular 中检测自定义表单控件上的提交事件 - detect submit event on custom form control in Angular 如何在Angular 2中使用RXJS观察自定义事件? - How to Observe a Custom Event using RXJS in Angular 2? 如何使用 CdkScrollable 检测角度材料 2 自动完成列表上的滚动事件 - How to detect scroll event on angular material 2 autocomplete list using CdkScrollable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM