2016-11-04 112 views
4

我正在嘗試使用ng2-bootstrap的模態。所以我配置一切等於示例如下:在Angular2中導入ng2-bootstrap模態

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

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

但是我還是發現了以下錯誤:

Cannot read property 'show' of undefined

組件:

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

模板:

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

我該如何解決這個問題?

回答

2

模板變量區分大小寫,在您的情況下它是#lgmodal(小寫字母「m」),並且您試圖在點擊時顯示lgModal(大寫「M」):(click)="lgModal.show()"。所以,只需更改您的模板變量,使其與(click)事件#lgModal中的模板變量匹配。

+2

當然,這是正確的!將html轉換爲帕格時出現一個愚蠢的錯誤。謝謝 :) –