ng-bootstrap无法在角度4中工作

时间:2017-07-08 22:18:09

标签: twitter-bootstrap angular bootstrap-4 ng-bootstrap angular4-forms

我是angular 4的新手,我正在尝试配置bootstrap。 我安装了ng-bootstrap:

https://ng-bootstrap.github.io/#/getting-started

我在页面上做了所有的事情,但我没有在页面上看到引导程序。 这是我的代码:

的src /应用程序/ app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { FormsModule } from '@angular/forms';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';

@NgModule({
  declarations: [
    AppComponent
  ],
imports: [
   BrowserModule,
   FormsModule,  // add  this
   NgbModule.forRoot()
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

的src /应用程序/ app.component.html

<div class="container">

<h1 class="text-primary">{{title}} </h1>

<h2 class="text-primary">My Heroes</h2>
<ul class="heroes">
  <li *ngFor="let hero of heroes"
    [class.selected]="hero === selectedHero"
    (click)="onSelect(hero)">
    <span class="badge">{{hero.id}}</span> {{hero.name}}
   </li>
</ul>

<div class="alert alert-success" role="alert">
 <strong>Well done!</strong> You successfully read this important alert 
   message.
</div>

<div *ngIf="selectedHero">
  <h2>{{selectedHero.name}} details!</h2>
<div><label>id: </label>{{selectedHero.id}}</div>
<div>
  <label>name: </label>
  <input [(ngModel)]="selectedHero.name" placeholder="name"/>
</div>

我还在index.html中尝试了一些代码,但它不起作用,

对于这一行,我期待看到一些颜色:

<h1 class="text-primary">{{title}} </h1>

当我检查html的标题时,我找不到任何引导程序的引用。 有什么帮助吗? 感谢

2 个答案:

答案 0 :(得分:30)

在你提到的页面中,ng-bootstrap提到bootstrap CSS作为依赖项。这样就可以单独加载了。

如果您使用Angular CLI创建应用程序,可以follow this official guide添加它,

更新

正如评论中所述,在styles(或此文件的任何其他更改)中将CSS添加到.angular-cli.json将要求您重新启动ng serveng build --watch如果你已经在运行。

更新2:

对于Angular 6及更高版本,您可以使用此解决方案。

转到styles.css并添加此行

@import "~bootstrap/dist/css/bootstrap.css";

另请参阅如何在幕后工作:

https://blog.angularindepth.com/this-is-how-angular-cli-webpack-delivers-your-css-styles-to-the-client-d4adf15c4975

答案 1 :(得分:1)

我的代码中没有看到任何ng-bootstrap组件!我认为你正在寻找bootstrap css,这也是ng-bootstrap的先决条件。

index.html标记:

之间的<head></head>中添加这些行
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
相关问题