ngx-bootstrap错误类型错误:_co.openModal不是函数

时间:2017-10-23 09:54:40

标签: html angular function ngx-bootstrap

我是ngx-bootstrap的新手,我尝试从这个网站制作模态:https://valor-software.com/ngx-bootstrap/#/modals

这我得到了这个错误: ERROR TypeError:_co.openModal不是函数

这里有一些片段



//app.component.ts file

import { Component } from '@angular/core';
import { TemplateRef } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/modal-options.class';


 @Component({
  selector: 'app-root',
  styleUrls:['app.component.css'],
  templateUrl:'app.component.html',
  //template:`<h1>Hello world</h1>`
})

export class AppComponent 
{
  title = 'ini tengah';
}

export class DemoModalServiceStaticComponent {
  public modalRef: BsModalRef;
  constructor(private modalService: BsModalService) {}
 
  public openModal(template: TemplateRef<any>) {
    this.modalRef = this.modalService.show(template);
  }
}
&#13;
<!--the HTML file-->

<!--call modal-->
<li><a href="#" (click)="openModal(template)">BUTTON</a></li>
<!--end-->

<!--Modal-->
<template #template>
    <div class="modal-header">
      <h4 class="modal-title pull-left">Modal</h4>
      <button type="button" class="close pull-right" aria-label="Close" (click)="modalRef.hide()">
        <span aria-hidden="true">&times;</span>
      </button>
    </div>
    <div class="modal-body">
      This is a modal.
    </div>
  </template>
<!--enf of modal-->
&#13;
&#13;
&#13;

我关注了网站,然后才得到了这个错误。任何人都可以帮我解决这个错误吗?

Plase让我知道什么时候需要更多的片段,提前谢谢你:)

1 个答案:

答案 0 :(得分:1)

试试这样:

在Appcomponent

中编写openModal方法
export class AppComponent {
  title = 'ini tengah';
  public modalRef: BsModalRef;
  constructor(private modalService: BsModalService) {}

  public openModal(template: TemplateRef<any>) {
    this.modalRef = this.modalService.show(template);
  }
}