Friday, April 19, 2019

How to add bootstrap , navbar, jquery, carousel to angular project





1>     Install bootstrap ,jquery, carousel


a.  npm install ngx-bootstrap bootstrap jquery 


b.      ng add ngx-bootstrap  --component carousel

c.  restart you vs code


2>     Add styles and scripts in angular.json

In angular.json

"styles": [

              "./node_modules/bootstrap/dist/css/bootstrap.min.css",

              "src/styles.css"

            ],

            "scripts": [

              "node_modules/jquery/dist/jquery.min.js",

              "node_modules/bootstrap/dist/js/bootstrap.min.js"

            ],

 
 

"assets": [

                "src/favicon.ico",

                "src/assets",

                "arc/assets/images"

          ]

3>     Create images folder in assets folder and copy all the required images in assets/images folder.

4>     Lets add carousels in our project (References : https://valor-software.com/ngx-bootstrap/#/carousel)

a.       Create a component -> ng g c components/carousels

b.      Add <app-carousels></app-carousels> in app.component.ts file

c.       Add the following code in ng7- carousels.component.ts file

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



@Component({

  selector: 'app-carousels',

  templateUrl: './carousels.component.html',

  styleUrls: ['./carousels.component.css']

})

export class CarouselsComponent implements OnInit {



  constructor() { }



  ngOnInit() {

  }

  slides = [

    {image: 'assets/images/1.jpg'},

    {image: 'assets/images/2.jpg'},

    {image: 'assets/images/3.jpg'}

  ];

}



d.      Add the following code in carousels.component.html file

<carousel [showIndicators]="showIndicator">

    <slide *ngFor="let slide of slides; let index=index">

        <div class="carousel-item active">

                <img [src]="slide.image" alt="image slide" style="width:100%" height="300px">

                <div class=" carousel-caption">

                    <h4>image {{index+1}}</h4>

                    <p>{{slide.text}}</p>

                </div>

        </div>

    </slide>

</carousel>



5>  Lets create navbar -> goto the following link for bootstrap component ref and select navbar from the right slide


6>  Copy the navbar code and remove the dropdown code



7>     Create a component -> ng g c components/navbar

8>     Add <app-navbar></app-navbar>in app.component.ts file

9>     in ng7- navbar.component.ts file

10>You can copy the following code : I have rename navbar-brand to Destination tours & Travels, you can give your own company name.

11>Even change the link Text – About, home, login etc according to your project requirement.



<nav class="navbar navbar-inverse">

    <div class="container-fluid">

        <!-- Brand and toggle get grouped for better mobile display -->

        <div class="navbar-header">

            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"

                data-target="#bs-example-navbar-collapse-1" aria-expanded="false">

                <span class="sr-only"></span>

                <span class="icon-bar"></span>

                <span class="icon-bar"></span>

                <span class="icon-bar"></span>

            </button>

            <a class="navbar-brand" href="#">Destination Tours & Travels</a>

        </div>



        <!-- Collect the nav links, forms, and other content for toggling -->

        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">

            <ul class="nav navbar-nav">

                <li class="active"><a href="#">Home <span class="sr-only">(current)</span></a></li>

                <li><a href="#"> About </a></li>

                <li><a href="#"> Contact Us </a></li>

                <li><a href="#"> Login </a></li>

                <li><a href="#"> Sign Up </a></li>

            </ul>

            <form class="navbar-form navbar-left">

                <div class="form-group">

                    <input type="text" class="form-control" placeholder="Search">

                </div>

                <button type="submit" class="btn btn-default">Submit</button>

            </form>

        </div><!-- /.navbar-collapse -->



    </div><!-- /.container-fluid -->

</nav>





Done 😊 now run your application -> Happy Learning 

No comments:

Post a Comment