Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.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
Angular 如何使用EventEmitter输出和订阅从子函数调用父函数_Angular_Modal Dialog_Ngx Bootstrap_Ngx Datatable - Fatal编程技术网

Angular 如何使用EventEmitter输出和订阅从子函数调用父函数

Angular 如何使用EventEmitter输出和订阅从子函数调用父函数,angular,modal-dialog,ngx-bootstrap,ngx-datatable,Angular,Modal Dialog,Ngx Bootstrap,Ngx Datatable,我需要使用事件从modalComponent刷新表 子组件中的 @Output() callrefresh = new EventEmitter<string>(); this.service.save(this.car).subscribe( (result) => { this.closeModal(); this.callrefresh .emit('I want to call a function');

我需要使用事件从modalComponent刷新表

子组件中的

 @Output() callrefresh = new EventEmitter<string>();
this.service.save(this.car).subscribe(
        (result) => {
          this.closeModal();
          this.callrefresh .emit('I want to call a function');
        },
 refreshTable(){ 
this.tableRefresh();
}


     tableRefresh(      
    page?: Page
  ): void {
    this.subscriptions.push(
      this.service
        .findAlldata(
          this.sort.dir,
          this.page.pageNumber,
          this.page.size,
          this.sort.prop
        )
        .subscribe({
          next: (data: any) => {
           all paged data logic
            );              
          },
          error: (error: any) => {
            console.log(error);
          },
          complete: () => {
            console.log('complete');
          },
        })
    );
  }
在父组件中,我想激活此功能

    refreshTable(){ 
    this.tableRefresh();
    }
有什么帮助吗?举个例子就好了

在父组件中有一个表,在子组件中有一个模态。当我在子组件中保存数据行时,我希望在父组件中激活刷新功能

例如,在子组件中

 @Output() callrefresh = new EventEmitter<string>();
this.service.save(this.car).subscribe(
        (result) => {
          this.closeModal();
          this.callrefresh .emit('I want to call a function');
        },
 refreshTable(){ 
this.tableRefresh();
}


     tableRefresh(      
    page?: Page
  ): void {
    this.subscriptions.push(
      this.service
        .findAlldata(
          this.sort.dir,
          this.page.pageNumber,
          this.page.size,
          this.sort.prop
        )
        .subscribe({
          next: (data: any) => {
           all paged data logic
            );              
          },
          error: (error: any) => {
            console.log(error);
          },
          complete: () => {
            console.log('complete');
          },
        })
    );
  }
在父组件中

 @Output() callrefresh = new EventEmitter<string>();
this.service.save(this.car).subscribe(
        (result) => {
          this.closeModal();
          this.callrefresh .emit('I want to call a function');
        },
 refreshTable(){ 
this.tableRefresh();
}


     tableRefresh(      
    page?: Page
  ): void {
    this.subscriptions.push(
      this.service
        .findAlldata(
          this.sort.dir,
          this.page.pageNumber,
          this.page.size,
          this.sort.prop
        )
        .subscribe({
          next: (data: any) => {
           all paged data logic
            );              
          },
          error: (error: any) => {
            console.log(error);
          },
          complete: () => {
            console.log('complete');
          },
        })
    );
  }

您只需使用由输出触发的事件。发出时,将调用refreshTable()

<child (callrefresh)="refreshTable()"></child>


能否请您添加更多详细信息,您的意图是什么,您希望从哪个组件访问什么,并为您希望交互的两个组件添加更多代码。或者一个stackblitz也可以。请发布相关的模板。请确保您的问题中包含一个“不工作”,您能给我一个完整的示例吗?这正是输出的工作原理,因此,如果它不工作,那是因为您的refreshTable不工作。使用subscribe,child hashtag会断开表viewChild不存在,因为它只是一个示例,您需要使用实际的组件Selectories,我使用了我的组件选择器,但不起作用