简体   繁体   中英

Importing ng2-bootstrap modal in Angular2

I'm trying to use modal of ng2-bootstrap . So I configured everything equals to the examples as below:

import { ModalModule } from 'ng2-bootstrap/ng2-bootstrap';

@NgModule({
    bootstrap: [ AppComponent ],
    declarations: [
        AppComponent,
        ...
    ],
    imports: [
        BrowserModule,
        FormsModule,
        HttpModule,
        RouterModule.forRoot(ROUTES),
        ModalModule
    ],
    providers: [ ]
})

However I'm still getting the following error:

Cannot read property 'show' of undefined

Component:

import { ViewContainerRef } from '@angular/core';

@Component({
    selector: 'any-comp',
    encapsulation: ViewEncapsulation.None,
    templateUrl: './any-comp.component.html'
})
export class AnyCompComponent implements OnInit {

private viewContainerRef: ViewContainerRef;

constructor(
    private pageContentService: PageContentService,
    viewContainerRef: ViewContainerRef) {
    this.viewContainerRef = viewContainerRef;
}

Template:

<button class="btn btn-primary" (click)="lgModal.show()">Large modal</button>

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

How can I fix this?

Template variables are case sensitive, in your case it is #lgmodal (lowercase "m") and you are trying to show lgModal on click (uppercase "M"): (click)="lgModal.show()" . So, just change your template variable so it matches the one from (click) event: #lgModal .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM