1

I'm having some trouble adding bootstrap to my angular2 project.

As documented on https://valor-software.com/ng2-bootstrap/ I've installed ng-bootstrap to the node-modules directory by npm install --save ng2-bootstrap (moment is included in the same directory).

Adding

import {Component, ChangeDetectionStrategy} from '@angular/core';
import {CORE_DIRECTIVES} from '@angular/common';
import {TAB_DIRECTIVES} from '@ng2-bootstrap';

leads to the following error error TS2307: Cannot find module '@ng2-bootstrap'. Also changed the path from '../../../ng2-bootstrap' to '@ng2-bootstrap'.

Both did not work. any ideas how to fix it?

4
  • Try like this - import { TAB_DIRECTIVES } from 'ng2-bootstrap/ng2-bootstrap'; Commented Aug 31, 2016 at 13:19
  • tried this. no error in the terminal, but after rebuilding this one appears in the firebug-console "NetworkError: 404 Not Found - http://localhost:8443/ng2-bootstrap/ng2-bootstrap" Commented Aug 31, 2016 at 13:28
  • I use the latest version of ng2-bootstrap too and import { TAB_DIRECTIVES } from 'ng2-bootstrap/ng2-bootstrap'; works fine for me. Did you check if the node_modules folder indeed contains /ng2-bootstrap/ng2-bootstrap.js and d.ts file Commented Aug 31, 2016 at 13:50
  • ng2-bootstrap, was renamed to ngx-bootstrap Commented Sep 13, 2017 at 8:57

2 Answers 2

2

After you ran npm install --save ng2-bootstrap, you need follow these steps-

In Systemjs.config.js, configure ng2-bootstrap like this-

  var map = {
    'app':                        'app', // 'dist',
    '@angular':                   'node_modules/@angular',
    'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
    'rxjs':                       'node_modules/rxjs',
    'ng2-bootstrap':              'node_modules/ng2-bootstrap'
  };

  // packages tells the System loader how to load when no filename and/or no extension
  var packages = {
    'app':                        { main: 'main.js',  defaultExtension: 'js' },
    'rxjs':                       { defaultExtension: 'js' },
    'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' },
    'ng2-bootstrap': { main: 'ng2-bootstrap.js', defaultExtension:'js'}
  };

In index.html add these

<script src="node_modules/ng2-bootstrap/bundles/ng2-bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">

In your component you can use like this-

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

Sample implementation:

import { Component } from '@angular/core';
import {CORE_DIRECTIVES} from '@angular/common';
import {TAB_DIRECTIVES } from 'ng2-bootstrap/ng2-bootstrap';

@Component({
  selector: 'my-app',
   directives: [TAB_DIRECTIVES, CORE_DIRECTIVES],
  template: `<h1>My First Angular 2 App with ng2-bootstrap</h1>

    <tabset>
    <tab heading="Static title">Static content</tab>
    <tab *ngFor="let tabz of tabs"
         [heading]="tabz.title"
         [active]="tabz.active"
         (select)="tabz.active = true"
         (deselect)="tabz.active = false"
         [disabled]="tabz.disabled"
         [removable]="tabz.removable"
         (removed)="removeTabHandler(tabz)">
      {{tabz?.content}}
    </tab>
  </tabset>

  `
})
export class AppComponent {
    public tabs:Array<any> = [
    {title: 'Dynamic Title 1', content: 'Dynamic content 1'},
    {title: 'Dynamic Title 2', content: 'Dynamic content 2', disabled: true},
    {title: 'Dynamic Title 3', content: 'Dynamic content 3', removable: true}
  ];
 }

NOTE: Latest ng2-bootstrap version is compatible with angular2 RC4 version.

See if this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you. I forgot to include ng2-bootstrap in the packages array.
1

UPDATE: Here's an interesting article on how to implement bootstraps latest version 4 Alpha into your angular project with ng-cli. https://angularjs.de/artikel/angular2-bootstrap-scss-angular-cli

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.