yooouuri
yooouuri

Reputation: 2658

Cannot read property 'show' of undefined

I have two components, customer and customers component. The customers components holds a list of customers and when i click on a customer, the customer component should open a modal.

customers.component.ts

import { Component, OnInit, } from '@angular/core';

import { CustomerComponent } from '../customer/customer.component';

export class Customer {
  id: number;
  name: String;
}

const CUSTOMERS: Customer[] = [
  {
    id: 1,
    name: 'Customer one'
  },
  {
    id: 2,
    name: 'Customer two'
  }
];

@Component({
  selector: 'app-customers',
  templateUrl: './customers.component.html',
  styleUrls: ['./customers.component.css'],
  providers: [
    CustomerComponent
  ]
})
export class CustomersComponent implements OnInit {
  title: String;
  customers: Customer[] = CUSTOMERS;
  customerComponent: CustomerComponent;
  // or
  @ContentChild(CustomerComponent)
  public customerComponent: CustomerComponent;

  constructor() {
    this.title = 'Customers';

    this.customerComponent = new CustomerComponent();
    // tried removing
  }

  ngOnInit() {
  }

  showCustomer(customer: Customer) {
    this.customerComponent.openModal(customer);
  }
}

customers.component.html

<app-customer></app-customer>
<div id="page-wrapper">
  <div class="container-fluid">
    <div class="row">
      <div class="col-lg-12">
        <h1 class="page-header">{{title}}</h1>
      </div>
    </div>
    <div class="row">
      <div class="col-lg-12">
        <div class="panel panel-default">
          <div class="panel-heading">
            De klanten pagina
          </div>
          <div class="panel-body">
            <div class="table-responsive">
              <table class="table table-striped table-bordered table-hover">
                <thead>
                <tr>
                  <th>#</th>
                  <th>Name</th>
                </tr>
                </thead>
                <tbody *ngFor="let customer of customers;">
                <tr (click)="showCustomer(customer)" [style.cursor]="'pointer'">
                  <td>{{customer.id}}</td>
                  <td>{{customer.name}}</td>
                </tr>
                </tbody>
              </table>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

So after clicking on a tr, the CustomersComponent should show a modal.

customer.component.ts

import { Component, OnInit, ViewChild } from '@angular/core';
import { Customer } from '../customers/customers.component';
import { ModalDirective } from 'ngx-bootstrap';

@Component({
  selector: 'app-customer',
  templateUrl: './customer.component.html',
  styleUrls: ['./customer.component.css'],
})
export class CustomerComponent implements OnInit {
  @ViewChild('lgModal') public lgModal: ModalDirective;
  // or
  @ViewChild(ModalDirective) public lgModal: ModalDirective;

  constructor() {
  }

  ngOnInit() {
  }

  openModal(customer: Customer): void {
    console.log(customer);
    console.log(this.lgModal);

    this.lgModal.show();
  }
}

And the customer.component.html

<div bsModal #lgModal="bs-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title pull-left">Large modal</h4>
        <button type="button" class="close pull-right" (click)="lgModal.hide()" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        ...
      </div>
    </div>
  </div>
</div>

console.log(this.lgModal); returns undefined and this.lgModal.show(); gives an ERROR TypeError: Cannot read property 'show' of undefined error..

edit:

Tried to add <app-customer></app-customer> to customers.component.html

Upvotes: 1

Views: 20888

Answers (3)

Ramona Stoica
Ramona Stoica

Reputation: 19

I had a similar problem. The error shown was undefined and Cannot read property 'hide' of undefined... I solved it by adding in the (child) constructor my modal as below which was implemented in the parent component.

constructor(public openModalDetails: BsModalRef) { }

Upvotes: 0

AVJT82
AVJT82

Reputation: 73367

I found a solution for your problem, thanks to this issue: https://www.bountysource.com/issues/35264857-how-to-call-modal-from-parent-component

Changes made is to have the child as exportAs: 'child' (for example)

@Component({
  ....
  exportAs: 'child'
})

Then in parent template have:

<app-customer #c="child"></app-customer>

and in parent template you can directly call your method in child if you like:

(click)="c.openModal(customer)" 

and in child you declare your @ViewChild:

@ViewChild('lgModal') public lgModal: ModalDirective;

That should do it!

Here's a DEMO to play with :)

Upvotes: 5

Reactgular
Reactgular

Reputation: 54811

@ViewChild and the other content query bindings use the constructor function of a component to find matches. When you pass the string it's assuming you're referring to a dependency injection token.

You need to change the code to this:

 @ViewChild(ModalDirective) public lgModal: ModalDirective;

Note: You can not query abstract classes or interfaces. It has to be the type of the component or directive.

Edit:

You can not create components this way:

this.customerComponent = new CustomerComponent();

This will not work as Angular needs to be used to create components. Try adding the customer component to the template of customers, and then use @ContentChild to access that component.

@ContentChild(CustomerComponent)
public customerComponent: CustomerComponent;

The reference for @ContentChild will be set AfterContentInit.

Upvotes: 0

Related Questions