angular - NG-bootstrap 模态组件无提供者错误

标签 angular ng-bootstrap

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

没有 NgbModal 的提供程序,我在示例代码中没有看到任何内容。我不确定在哪里可以设置一个 Angular 2 项目来遗憾地进行演示。我想做的就是采用他们的示例代码并将其实现到 ng new <projectname> 的标准项目中将生成 modile.component.tsapp.component.ts文件。

app.module.ts

import { 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.component.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.html

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

modal.component.ts

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

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

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

最佳答案

您必须将 NgbModule 导入到 NgModuleimports 数组中。

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

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

关于angular - NG-bootstrap 模态组件无提供者错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44211922/

相关文章:

css - 如何更改轮播中图标的颜色?

javascript - 无法关闭 ng-bootstrap 模式

node.js - 错误 TS2305 : Module has no exported member

angular - 如何在 Angular 5 中使用订阅进行单元测试?

angular - Uncaught Error : Cannot find module "tslib"

angular - 如何将 'on' 、 'off' 标签添加到 mat-slide-toggle ?

angular - 将日期范围添加到 NgbInputDatepicker 输入字段

angular - 注销时关闭所有 ng-bootstrap 模式

css - 移除 Material Stepper header

angular - angular2中的注解和装饰器有什么区别