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

Paul

I have a dialog subcomponent DeleteAssociationDialog with an openDeleteAssociationDialog method in it:

delete-association-dialog.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');
    });
  }
}

The dialog should be shown when clicked on a button in the parent component (app.component) HTML, I am using @ViewChild to establish a reference:

app.component.html [fragment]

<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;
}

Getting an error -- "ERROR TypeError: Cannot read property 'openDeleteAssociationDialog' of undefined"

What am I missing? How do I properly refer to a subcomponent method from a parent component HTML template?

vince

I think the problem stems from the fact that the method to open the dialog is contained within the dialog itself. So that method won't exist unless the dialog is already opened...the dialog component is both the chicken and the egg.

The solution is to move the method to openDeleteAssociationDialogComponent to the parent.

And then it's simple:

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

If you want to abstract it away to make the dialog-opening button reusable, you can do something like:

component.html

<association-deleter></association-deleter>

component.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');
    });
  }
}

Then you could reuse the button which opens the delete association dialog.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to refer to parent component from a Directive in angular 5 using dynamic components

From Dev

Angular 4 passing a value from html template to a method in a component

From Dev

Angular 5 how to add ngClass of component's elements from parent HTML

From Javascript

How to inherit css styles in child component from parent in Angular 5

From Dev

How to pass a custom (html) template to child component via parent component in angular 2?

From Dev

How to edit a shared component from parent's HTML in angular 6

From Dev

how to display my method from component to html on angular

From Dev

Angular 5: creating a parent component with an external template and overriding the inner part

From Dev

How can I pass a function as a parameter in an angular HTML template to update a variable in the parent component?

From Dev

How to send data from parent component to child component without using *ngFor in angular 5

From Dev

How to switch component in angular 5 like template?

From Dev

Angular 14 calling Child Component method from Parent Component

From Dev

VueJS How to use parent method from component

From Dev

Angular 5 Update Parent Component value from child Component

From Javascript

Pass Event from child component to parent component Angular 5

From Dev

How to get html template of an Angular 2 Component?

From Dev

How to get component from template method in ECS?

From Dev

Angular 2 how to emit method in component child to component parent

From Dev

Styling child components template from parent component css angular

From Dev

How to refer overriden static method from parent class constructor?

From Dev

angular 4 - call child component function from parent component html

From Dev

Calling an angular component method from classic HTML

From Dev

Dagger 2 Adding a subcomponent to a parent component

From Dev

How To pass let-c="close" of modal template to other component's html angular 5

From Dev

Angular: How to navigate to child route from parent component by using navigate method

From Dev

How to call child components's method from the parent component in Angular 6

From Dev

How to call child component's method from a parent component in React

From Dev

How can I call method in child component from parent component?

From Dev

ReactJS. How to call component's method from parent component

Related Related

  1. 1

    How to refer to parent component from a Directive in angular 5 using dynamic components

  2. 2

    Angular 4 passing a value from html template to a method in a component

  3. 3

    Angular 5 how to add ngClass of component's elements from parent HTML

  4. 4

    How to inherit css styles in child component from parent in Angular 5

  5. 5

    How to pass a custom (html) template to child component via parent component in angular 2?

  6. 6

    How to edit a shared component from parent's HTML in angular 6

  7. 7

    how to display my method from component to html on angular

  8. 8

    Angular 5: creating a parent component with an external template and overriding the inner part

  9. 9

    How can I pass a function as a parameter in an angular HTML template to update a variable in the parent component?

  10. 10

    How to send data from parent component to child component without using *ngFor in angular 5

  11. 11

    How to switch component in angular 5 like template?

  12. 12

    Angular 14 calling Child Component method from Parent Component

  13. 13

    VueJS How to use parent method from component

  14. 14

    Angular 5 Update Parent Component value from child Component

  15. 15

    Pass Event from child component to parent component Angular 5

  16. 16

    How to get html template of an Angular 2 Component?

  17. 17

    How to get component from template method in ECS?

  18. 18

    Angular 2 how to emit method in component child to component parent

  19. 19

    Styling child components template from parent component css angular

  20. 20

    How to refer overriden static method from parent class constructor?

  21. 21

    angular 4 - call child component function from parent component html

  22. 22

    Calling an angular component method from classic HTML

  23. 23

    Dagger 2 Adding a subcomponent to a parent component

  24. 24

    How To pass let-c="close" of modal template to other component's html angular 5

  25. 25

    Angular: How to navigate to child route from parent component by using navigate method

  26. 26

    How to call child components's method from the parent component in Angular 6

  27. 27

    How to call child component's method from a parent component in React

  28. 28

    How can I call method in child component from parent component?

  29. 29

    ReactJS. How to call component's method from parent component

HotTag

Archive