NG-bootstrap模态组件没有提供程序错误

时间:2017-05-27 01:05:34

标签: angular ng-bootstrap

我是角度为2的ng-bootstrap库;特别是我在看:Components as content。我试图将示例代码合并到我的项目中,但收效甚微。我从浏览器调试器得到的错误是

没有NgbModal的提供者,我在示例代码中没有看到任何内容。我不知道在哪里可以设置一个角度2项目来悲伤地演示。我想做的就是拿出他们的示例代码并将其实现到modile.component.ts将使用app.component.tsimport { Component, NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { JsonpModule } from '@angular/http'; import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; import { AppComponent } from './app.component'; import { NgbdModalComponent, NgbdModalContent } from './modal.component'; @NgModule({ imports: [ BrowserModule, FormsModule ], declarations: [ AppComponent, NgbdModalComponent, NgbdModalContent ], entryComponents: [NgbdModalContent], bootstrap: [ AppComponent ] }) export class AppModule { } 文件生成的标准项目中。

app.module.ts

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'Tour of Heroes';
}

app.component.ts

  <div class="container-fluid">
    <hr>
    <p>
      This is a demo plnkr forked from the <strong>ng-bootstrap</strong> project: Angular powered Bootstrap.
      Visit <a href="https://ng-bootstrap.github.io/" target="_blank">https://ng-bootstrap.github.io</a> for more widgets and demos.
    </p>
    <hr>

    <ngbd-modal-component></ngbd-modal-component>
  </div>

app.component.html

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

import {NgbModal, NgbActiveModal} from '@ng-bootstrap/ng-bootstrap';

@Component({
  selector: 'ngbd-modal-content',
  template: `
    <div class="modal-header">
      <h4 class="modal-title">Hi there!</h4>
      <button type="button" class="close" aria-label="Close" (click)="activeModal.dismiss('Cross click')">
        <span aria-hidden="true">&times;</span>
      </button>
    </div>
    <div class="modal-body">
      <p>Hello, {{name}}!</p>
    </div>NgbdModalComponent
    <div class="modal-footer">
      <button type="button" class="btn btn-secondary" (click)="activeModal.close('Close click')">Close</button>
    </div>
  `
})
export class NgbdModalContent {
  @Input() name;

  constructor(public activeModal: NgbActiveModal) {}
}

@Component({
  selector: 'ngbd-modal-component',
  templateUrl: './modal.component.html'
})
export class NgbdModalComponent {
  constructor(private modalService: NgbModal) {}

  open() {
    const modalRef = this.modalService.open(NgbdModalContent);
    modalRef.componentInstance.name = 'World';
  }
}

modal.component.ts

<p>You can pass an existing component as content of the modal window. In this case remember to add content component
as an <code>entryComponents</code> section of your <code>NgModule</code>.</p>

<button class="btn btn-lg btn-outline-primary" (click)="open()">Launch demo modal</button>

modal.component.html

{{1}}

错误:没有NgbModal,错误上下文,未处理承诺拒绝的提供者:没有NgbModal的提供者。感谢您的反馈。

3 个答案:

答案 0 :(得分:5)

您必须将NgbModule导入NgModule导入数组。

import {NgbModule} from '@ng-bootstrap/ng-bootstrap';

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

答案 1 :(得分:0)

以防您使用延迟加载。将NgbModule导入进口数组也很重要。但这次不是像@Pengyy所说的那样,而是

create table c_order(
    order_date         TIMESTAMP not null DEFAULT CURRENT_TIMESTAMP,
    order_id           number,
    customer_id        number             not null,
    product_id         number             not null,
    primary key (order_id),
    foreign key (customer_id) references Customer(customer_id),
    foreign key (product_id) references products(product_id)
);

答案 2 :(得分:0)

尝试在组件上提供NgbActiveModal

providers: [
    NgbActiveModal,
]