Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将自定义事件绑定到angular中的父元素?_Javascript_Angular_Angular8 - Fatal编程技术网

Javascript 如何将自定义事件绑定到angular中的父元素?

Javascript 如何将自定义事件绑定到angular中的父元素?,javascript,angular,angular8,Javascript,Angular,Angular8,我正在使用该工具来侦听我的项目的大小调整 因此,我可以使用 但我需要在嵌套组件中使用它来侦听父级 我在我的WidgetComponentinit函数中使用我的ChartComponent,如下所示: ChartComponent的初始函数为: ngOnInit() { let chartComponent = this.elementRef.nativeElement.parentElement; let widgetContainerComponent = chartCom

我正在使用该工具来侦听我的项目的大小调整

因此,我可以使用

但我需要在嵌套组件中使用它来侦听父级

我在我的
WidgetComponent
init函数中使用我的
ChartComponent
,如下所示:

ChartComponent的初始函数为:

ngOnInit() {
     let chartComponent = this.elementRef.nativeElement.parentElement;
     let widgetContainerComponent = chartComponent.parentElement;
     let cardBody = widgetContainerComponent.parentElement;

     this.renderer.listen(cardBody, 'resized', (event) => {
       console.log(event)
    })
}
但这是行不通的。但如果我添加click listener,它就可以工作了

    this.renderer.listen(cardBody, 'click', (event) => {
       console.log(event)
    })
我想从子组件中侦听已调整大小的父
事件。如果父级大小更改,我将在子组件(ChartComponent)中设置图表的宽度和高度


如何设置已调整大小的
事件?

如果希望子组件从父组件侦听,可以使用EventEmitter。 一个例子

在子组件上添加EventEmitter:

@Input() private uploadSuccess: EventEmitter<boolean>;
父组件

在ParentComponent.html中

<child-component  [uploadSuccess] = "uploadSuccess" > </child-component>

在ParentComponent.ts中

private uploadSuccess: EventEmitter<boolean> = new EventEmitter();

  onImageUploadSuccess(event) {
    console.log('Image Upload succes');
    if (event.code === 'OK' && this.maxUploadLimit > 0) {
      this.galleryImagesCount = this.galleryImagesCount + 1;
      this.maxUploadLimit = this.maxUploadLimit - 1;
     }

      // The parent emits the event which was given as `@Input` variable to the child- 
   component
     this.uploadSuccess.emit(true);
   }
private uploadSuccess:EventEmitter=neweventemitter();
onImageUploadSuccess(事件){
console.log('Image Upload success');
if(event.code==='OK'&&this.maxUploadLimit>0){
this.gallerymagesunt=this.gallerymagesunt+1;
this.maxUploadLimit=this.maxUploadLimit-1;
}
//父对象向子对象发出作为“@Input”变量给定的事件-
组成部分
this.uploadSuccess.emit(true);
}

所以您希望您的子组件先侦听来自父组件的事件,然后再侦听来自子组件的已调整大小的父事件。如果父级大小更改,我将设置子组件的图表。
private uploadSuccess: EventEmitter<boolean> = new EventEmitter();

  onImageUploadSuccess(event) {
    console.log('Image Upload succes');
    if (event.code === 'OK' && this.maxUploadLimit > 0) {
      this.galleryImagesCount = this.galleryImagesCount + 1;
      this.maxUploadLimit = this.maxUploadLimit - 1;
     }

      // The parent emits the event which was given as `@Input` variable to the child- 
   component
     this.uploadSuccess.emit(true);
   }