Cannot read property 'backdrop' of undefined

Viewed 11015

This is code I'm using in my HTML page. In my angular module I import every relevant things. But always says this error because of backdrop. Please help. I have no idea about this backdrop.If some can explain please what is this error and why it comes ?

<div class="animated fadeIn">
      <div class="row">
        <div class="col-lg-12">
          <div class="card">
            <div class="card-header">
              <i class="fa fa-align-justify"></i> Bootstrap Modals
            </div>
            <div class="card-block">
              <!-- Button trigger modal -->
              <button type="button" class="btn btn-secondary" data-toggle="modal" (click)="smallModal.show()">
        Launch small modal
      </button>
            </div>
          </div>
        </div>
        <!--/.col-->
      </div>
      <!--/.row-->
    </div>

    <div bsModal #smallModal="bs-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog modal-sm" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h4 class="modal-title">Modal title</h4>
            <button type="button" class="close" (click)="smallModal.hide()" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
          </div>
          <div class="modal-body">
            <p>One fine body&hellip;</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" (click)="smallModal.hide()">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
        <!-- /.modal-content -->
      </div>
      <!-- /.modal-dialog -->
    </div>
    <!-- /.modal -->

This is my .ts file

import { Component, OnInit, ViewChild } from '@angular/core';
import { ModalDirective } from 'ng2-bootstrap/modal/modal.component';

@Component({
  selector: 'app-mso-user-profile',
  templateUrl: './mso-user-profile.component.html',
  styleUrls: ['./mso-user-profile.component.css']
})
export class MsoUserProfileComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}
2 Answers

If you are trying to load component dynamically by factory like below:

const factory = this.resolver.resolveComponentFactory(YOURModalComponent);
const componentRef = this.fileAttacherContainer.createComponent(factory);
const component = componentRef.instance;

You have to wait till the component creation complete by the factory resolver, I mean when the component doesn't create yet then initialize of ngx-bootstrap backdrop not called, for example if you call your show modal like below everything goes well:

setTimeout(() => {
    component.show();
}, 2000); //any millisecond

But, do not forget this is a bad solution, but I told for your better understanding.

Finally, the good solution is using Promises or simply call component.show(); in ngAfterViewInit of your child component.

By the way, read this article too: https://angular.io/api/core/AfterViewChecked

import this class:

// RECOMMENDED
import { ModalModule } from 'ngx-bootstrap/modal';
// or
import { ModalModule } from 'ngx-bootstrap';

@NgModule({
  imports: [ModalModule.forRoot(),...]
})
export class AppModule(){}
Related