Angular 5 : how to refer to a subcomponent method from a parent component HTML template?

标签 angular typescript angular5

我有一个对话框子组件DeleteAssociationDialog,其中有一个openDeleteAssociationDialog方法:

删除关联对话框.component.ts

import { Component } from '@angular/core';
import { MatDialog, MatDialogRef } from '@angular/material';

@Component({
  selector: 'app-delete-association-dialog',
  templateUrl: 'delete-association-dialog.component.html',
  styleUrls: ['delete-association-dialog.component.css']
})
export class DeleteAssociationDialogComponent {

  constructor(
  public dialog: MatDialog,
  public dialogRef: MatDialogRef<DeleteAssociationDialogComponent>) { }

  openDeleteAssociationDialog(): void {
    let dialogRef = this.dialog.open(DeleteAssociationDialogComponent, {
      width: '250px'
    });

    dialogRef.afterClosed().subscribe(result => {
      console.log('The dialog was closed');
    });
  }
}

当单击父组件(app.component)HTML 中的按钮时,应该显示该对话框,我使用 @ViewChild 来建立引用:

app.component.html [片段]

<button mat-icon-button color="warn" (click)="child.openDeleteAssociationDialog()">
  <mat-icon>delete</mat-icon>
</button>

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatButtonModule } from '@angular/material/button';
import { MatInputModule } from '@angular/material';
import { FormsModule } from '@angular/forms';
import { MatDialogModule } from '@angular/material';

import { AppComponent } from './app.component';
import { DeleteAssociationDialogComponent } from './delete-association-dialog/delete-association-dialog.component';

import { MatDialogRef} from '@angular/material/dialog'

@NgModule({
  declarations: [
    AppComponent,
    DeleteAssociationDialogComponent,
  ],
  entryComponents: [DeleteAssociationDialogComponent],
  imports: [
    BrowserModule,
    NgModule,
    BrowserAnimationsModule,
    MatButtonModule,
    MatInputModule,
    FormsModule
    MatDialogModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts

import { Component, ViewChild } from '@angular/core';
import { MatDialog, MatDialogRef } from '@angular/material';
import { DeleteAssociationDialogComponent } from './delete-association-dialog/delete-association-dialog.component';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css', './app.component.panel.css']
})
export class AppComponent {
  @ViewChild('DeleteAssociationDialogComponent') child: DeleteAssociationDialogComponent;
}

出现错误-- “错误类型错误:无法读取未定义的属性‘openDeleteAssociationDialog’”

我错过了什么?如何从父组件 HTML 模板中正确引用子组件方法

最佳答案

我认为问题源于这样一个事实:打开对话框的方法包含在对话框本身中。因此,除非对话框已经打开,否则该方法不会存在...对话框组件既是鸡又是蛋。

解决方案是将 openDeleteAssociationDialogComponent 方法移至父级。

然后就很简单了:

<button (click)="openDeleteAssociationDialogComponent()"></button>

如果您想将其抽象出来以使对话框打开按钮可重用,您可以执行以下操作:

组件.html

<association-deleter></association-deleter>

组件.ts

@Component({
  selector: 'association-deleter',
  template: `<button (click)="openDialog()"></button>`
})
export class DeleteAssociationComponent {
  constructor(
    public dialog: MatDialog,
    public dialogRef: MatDialogRef<DeleteAssociationDialogComponent>
  ){}

  openDeleteAssociationDialog(): void {
    let dialogRef = this.dialog.open(DeleteAssociationDialogComponent, {
      width: '250px'
    });

    dialogRef.afterClosed().subscribe(result => {
      console.log('The dialog was closed');
    });
  }
}

然后您可以重复使用打开删除关联对话框的按钮。

关于 Angular 5 : how to refer to a subcomponent method from a parent component HTML template?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49301626/

相关文章:

typescript - 如何在单独的文件中声明和导入 typescript 接口(interface)

css - 在 Angular Material 表中使用省略号

Angular 2 : Just update the last segment of a route

svg - 组件单元测试 : By. css() 在 SVG 命名空间中时不匹配

typescript - Vue + Typescript - 无效类型

Angular 5 和 NPM

javascript - Angular 5+构造函数字段注入(inject)器错误

javascript - 如何从 Observable (rxjs) 获取更改的元素

javascript - 通过将数据存储在类变量中来间接更改 UI 不起作用

angular - mat-autocomplete 过滤器以突出显示部分字符串匹配