Zack Jackson

Zack Jackson

1575519334

Building Responsive Carousel in Angular 9 Application with NgBootstrap

A comprehensive step by step tutorial on building responsive Carousel in Angular 9 application with NgBootstrap package.

Carousels are used on the front of the web or mobile applications, Carousels are cool and helpful in displaying image galleries, sell products, show related content, showing repetitive content, attract the attention of new visitors

There are multiple Angular Carousel packages available online to display the repetitive data on the front-end to the visitors.

In this tutorial, we will learn how to integrate responsive Bootstrap carousel in an Angular 8|9 application using the ng-bootstrap library.

NgBootstrap comes with built-in carousel exclusively for Angular, and this ready-made carousel is extremely simple to install and allow easy implementation of the following features.

  • Auto Rotate
  • Arrow navigation
  • Dynamic pagination
  • Rotating animations
  • Various slides with images and text

Table of Contents

  1. Setting Up Angular Project
  2. Configure Carousel Package in Angular
  3. Implementing Carousel in Angular 8/9
  4. Adding Animation to NgBootstrap Carousel
  5. Understanding NgbCarousel Methods
  6. Manage Carousel Behavior with NgbCarousel Methods
  7. Conclusion

Let’s get started.

1. Setting Up Angular Project

Make sure you have the latest version of Angular CLI installed on your device.

npm install -g @angular/cli@latest

If it throws any error then use the above command with sudo and provide admin password.

Use Angular CLI to generate a new Angular app, enter the command in the terminal and hit enter.

ng new angular-bootstrap-carousel

Get inside the project folder.

cd angular-bootstrap-carousel

2. Configure Carousel Package in Angular

To configure the NgBootstrap library we need to install Bootstrap library. Because NgBootstrap is just a UI component library for Bootstrap.

npm install --save @ng-bootstrap/ng-bootstrap

Next, we will install the ng-bootstrap module to configure the carousel in an Angular app.

npm install --save @ng-bootstrap/ng-bootstrap

Now, add the Bootstrap CSS in styles:[] array inside the angular.json file.

"styles": [
  "node_modules/bootstrap/dist/css/bootstrap.min.css"
]

Next, we will import NgbModule in main module file and also register NgbModule inside the imports array in app.module.ts file.

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

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

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

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    NgbModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})

export class AppModule { }

We have completed the configuration process. Next, run the app in the browser by running the following command.

ng serve --open

3. Implementing Responsive Carousel in Angular

To begin with ng-bootstrap Carousel, we define ngb-crousel directive.

To display every slide we use the ng-template directive and attach ngbSlide attribute with it. It acts as an individual slide in the Angular template.

To run the carousel, keep some images in the assets folder.

<ngb-carousel>
  <ng-template ngbSlide>
    <div class="picsum-img-wrapper">
      <img src="assets/img-01.jpeg" alt="Angular Carousel 1">
    </div>
    <div class="carousel-caption">
      <h3>Title Goes Here</h3>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    </div>
  </ng-template>
  <ng-template ngbSlide>
    <div class="picsum-img-wrapper">
      <img src="assets/img-02.jpeg" alt="Angular Carousel 2">
    </div>
    <div class="carousel-caption">
      <h3>Title Goes Here</h3>
      <p>Consectetur tortor volutpat pretium.</p>
    </div>
  </ng-template>
</ngb-carousel>

Angular Responsive Carousel

4. Adding Animation to NgBootstrap Carousel

NgBootstrap does not provide any official API for animating bootstrap carousel, however there are two ways through which we can add animation in Angular carousel. First, method is using Angular animation. Other method is by using CSS 3 animation, we are going to focus on the CSS method in this tutorial.

Add the following CSS in the styles.css file and it will add the animated fade-in effect in the Angular Carousel.

ngb-carousel {
    max-width: 700px;
    margin: 50px auto;
}

ngb-carousel img {
    width: 100%;
    outline: none;
}

ngb-carousel {
    width: inherit;
    height: inherit;
}

.carousel-inner {
    overflow: visible;
}

.carousel-item {
    display: flex !important;
    opacity: 0;
    visibility: hidden;
    transition: opacity 1.2s ease-in-out, visibility 1.2s;
    z-index: -1;
}

.carousel-item.active{
    opacity: 1;
    visibility: visible;
    z-index: 10;
}

.carousel-control-prev {
     z-index: 20;
}

.carousel-control-next {
     z-index: 20;
}

.carousel-indicators{
    z-index: 20;
}

5. Understanding NgbCarousel Methods

NgbCarousel is a component provided by NgBootstrap, and It helps in handling the setting of a Carousel in Angular. Below we will have a look at NgBootstrap’s Inputs, Outputs and Methods properties to deal with Bootstrap carousel.

NgbCarousel Inputs API

activeId: The slide id, which appears at the beginning.

interval: The time in milliseconds, within the duration slide goes to next.

Keyboard: The initial value is true, used to configure communication through the keyboard.

pauseOnHover: To stop carousel on when the mouse comes over the slides. The default value is true.

showNavigationArrows: If enable Next and Previous control arrows will be visible. However, the default is always true.

showNavigationIndicators: If the value is true Bottom indicators will be apparent if the value is set to true. However, the default value is true.

wrap: If the value is set to true, then the wrap property switches the last slide back to the first. However, the default value is true.

<ngb-carousel
[showNavigationArrows]="true" 
[showNavigationIndicators]="true"
interval="12000"
[keyboard]="true"
[pauseOnHover]="true"
[wrap]="true"
[activeId]="'secondSlide'"
>
  <ng-template ngbSlide id="firstSlide">
    <div class="picsum-img-wrapper">
      <img src="assets/img-01.jpeg" alt="Angular Carousel 1">
    </div>
    <div class="carousel-caption">
      <h3>Title Goes Here</h3>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    </div>
  </ng-template>
  <ng-template ngbSlide id="secondSlide">
    <div class="picsum-img-wrapper">
      <img src="assets/img-02.jpeg" alt="Angular Carousel 2">
    </div>
    <div class="carousel-caption">
      <h3>Title Goes Here</h3>
      <p>Consectetur tortor volutpat pretium.</p>
    </div>
  </ng-template>
</ngb-carousel>

Using Outputs API

slide: This event is triggered just after the slide transition is finished.

Define the (slide)="..." event with ngb-carousel directive in the Angular’s HTML template.

<ngb-carousel (slide)="slideActivate($event)">
  <ng-template ngbSlide>
    <div class="picsum-img-wrapper">
      <img src="assets/img-01.jpeg" alt="Angular Carousel 1">
    </div>
    <div class="carousel-caption">
      <h3>Title Goes Here</h3>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    </div>
  </ng-template>
  <ng-template ngbSlide>
    <div class="picsum-img-wrapper">
      <img src="assets/img-02.jpeg" alt="Angular Carousel 2">
    </div>
    <div class="carousel-caption">
      <h3>Title Goes Here</h3>
      <p>Consectetur tortor volutpat pretium.</p>
    </div>
  </ng-template>
</ngb-carousel>

Next, we require to import NgbSlideEvent and NgbSlideEventSource in app.component.ts. Afterwards, use the slide function to access the following events triggered by the following properties.

import { Component } from '@angular/core';
import { NgbSlideEvent, NgbSlideEventSource } from '@ng-bootstrap/ng-bootstrap';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {
  slideActivate(ngbSlideEvent: NgbSlideEvent) {
    console.log(ngbSlideEvent.source);
    console.log(ngbSlideEvent.paused);
    console.log(NgbSlideEventSource.INDICATOR);
    console.log(NgbSlideEventSource.ARROW_LEFT);
    console.log(NgbSlideEventSource.ARROW_RIGHT);
  }
}

6. Manage Carousel Behavior with NgbCarousel Methods

To handle Bootstrap Carousel behavior in Angular we use the following method offered by NgbCarousel.

select: Moves to a slide with provided slide id.

prev: Moves to the previous slide.

next: Navigates to the next slide.

pause: Stops the navigation of carousel slides.

cycle: Restarts the sliding from left to right in the carousel.

Next, we will learn how to use these output methods to ad the external events in the Angular Carousel.

Declare a template reference and bind it to the ngb-carousel and trigger the following method.

<ngb-carousel #ngcarousel>
  <ng-template ngbSlide id="one">
    <div class="picsum-img-wrapper">
      <img src="assets/img-01.jpeg" alt="Slide 1">
    </div>
  </ng-template>
  <ng-template ngbSlide id="two">
    <div class="picsum-img-wrapper">
      <img src="assets/img-02.jpeg" alt="Slide 2">
    </div>
  </ng-template>
  <ng-template ngbSlide id="three">
    <div class="picsum-img-wrapper">
      <img src="assets/img-03.jpeg" alt="Slide 3">
    </div>
  </ng-template>
  <ng-template ngbSlide id="four">
    <div class="picsum-img-wrapper">
      <img src="assets/img-04.jpeg" alt="Slide 4">
    </div>
  </ng-template>
</ngb-carousel>

<div class="text-center">
  <button (click)="navigateToSlide('four')">Go to Slide Four</button>
  <button (click)="getToPrev()">Prev</button>
  <button (click)="goToNext()">Next</button>
  <button (click)="stopCarousel()">Pause</button>
</div>

Next, trigger the Output methods in the Angular component.

import { Component, OnInit, ViewChild } from '@angular/core';
import { NgbCarousel } from '@ng-bootstrap/ng-bootstrap';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent implements OnInit {
  @ViewChild('ngcarousel', { static: true }) ngCarousel: NgbCarousel;

  ngOnInit() { }

  // Move to specific slide
  navigateToSlide(item) {
    this.ngCarousel.select(item);
    console.log(item)
  }

  // Move to previous slide
  getToPrev() {
    this.ngCarousel.prev();
  }

  // Move to next slide
  goToNext() {
    this.ngCarousel.next();
  }

  // Pause slide
  stopCarousel() {
    this.ngCarousel.pause();
  }

  // Restart carousel
  restartCarousel() {
    this.ngCarousel.cycle();
  }
}

7. Conclusion

Finally, we have completed Angular 8/9 Responsive Carousel tutorial with examples. I hope you liked this tutorial, please consider it sharing with others.

#Angular #Bootstrap #WebDev #JavaScript

What is GEEK

Buddha Community

Building Responsive Carousel in Angular 9 Application with NgBootstrap
Roberta  Ward

Roberta Ward

1595344320

Wondering how to upgrade your skills in the pandemic? Here's a simple way you can do it.

Corona Virus Pandemic has brought the world to a standstill.

Countries are on a major lockdown. Schools, colleges, theatres, gym, clubs, and all other public places are shut down, the country’s economy is suffering, human health is on stake, people are losing their jobs and nobody knows how worse it can get.

Since most of the places are on lockdown, and you are working from home or have enough time to nourish your skills, then you should use this time wisely! We always complain that we want some ‘time’ to learn and upgrade our knowledge but don’t get it due to our ‘busy schedules’. So, now is the time to make a ‘list of skills’ and learn and upgrade your skills at home!

And for the technology-loving people like us, Knoldus Techhub has already helped us a lot in doing it in a short span of time!

If you are still not aware of it, don’t worry as Georgia Byng has well said,

“No time is better than the present”

– Georgia Byng, a British children’s writer, illustrator, actress and film producer.

No matter if you are a developer (be it front-end or back-end) or a data scientisttester, or a DevOps person, or, a learner who has a keen interest in technology, Knoldus Techhub has brought it all for you under one common roof.

From technologies like Scala, spark, elastic-search to angular, go, machine learning, it has a total of 20 technologies with some recently added ones i.e. DAML, test automation, snowflake, and ionic.

How to upgrade your skills?

Every technology in Tech-hub has n number of templates. Once you click on any specific technology you’ll be able to see all the templates of that technology. Since these templates are downloadable, you need to provide your email to get the template downloadable link in your mail.

These templates helps you learn the practical implementation of a topic with so much of ease. Using these templates you can learn and kick-start your development in no time.

Apart from your learning, there are some out of the box templates, that can help provide the solution to your business problem that has all the basic dependencies/ implementations already plugged in. Tech hub names these templates as xlr8rs (pronounced as accelerators).

xlr8rs make your development real fast by just adding your core business logic to the template.

If you are looking for a template that’s not available, you can also request a template may be for learning or requesting for a solution to your business problem and tech-hub will connect with you to provide you the solution. Isn’t this helpful 🙂

Confused with which technology to start with?

To keep you updated, the Knoldus tech hub provides you with the information on the most trending technology and the most downloaded templates at present. This you’ll be informed and learn the one that’s most trending.

Since we believe:

“There’s always a scope of improvement“

If you still feel like it isn’t helping you in learning and development, you can provide your feedback in the feedback section in the bottom right corner of the website.

#ai #akka #akka-http #akka-streams #amazon ec2 #angular 6 #angular 9 #angular material #apache flink #apache kafka #apache spark #api testing #artificial intelligence #aws #aws services #big data and fast data #blockchain #css #daml #devops #elasticsearch #flink #functional programming #future #grpc #html #hybrid application development #ionic framework #java #java11 #kubernetes #lagom #microservices #ml # ai and data engineering #mlflow #mlops #mobile development #mongodb #non-blocking #nosql #play #play 2.4.x #play framework #python #react #reactive application #reactive architecture #reactive programming #rust #scala #scalatest #slick #software #spark #spring boot #sql #streaming #tech blogs #testing #user interface (ui) #web #web application #web designing #angular #coronavirus #daml #development #devops #elasticsearch #golang #ionic #java #kafka #knoldus #lagom #learn #machine learning #ml #pandemic #play framework #scala #skills #snowflake #spark streaming #techhub #technology #test automation #time management #upgrade

Christa  Stehr

Christa Stehr

1598940617

Install Angular - Angular Environment Setup Process

Angular is a TypeScript based framework that works in synchronization with HTML, CSS, and JavaScript. To work with angular, domain knowledge of these 3 is required.

  1. Installing Node.js and npm
  2. Installing Angular CLI
  3. Creating workspace
  4. Deploying your First App

In this article, you will get to know about the Angular Environment setup process. After reading this article, you will be able to install, setup, create, and launch your own application in Angular. So let’s start!!!

Angular environment setup

Install Angular in Easy Steps

For Installing Angular on your Machine, there are 2 prerequisites:

  • Node.js
  • npm Package Manager
Node.js

First you need to have Node.js installed as Angular require current, active LTS or maintenance LTS version of Node.js

Download and Install Node.js version suitable for your machine’s operating system.

Npm Package Manager

Angular, Angular CLI and Angular applications are dependent on npm packages. By installing Node.js, you have automatically installed the npm Package manager which will be the base for installing angular in your system. To check the presence of npm client and Angular version check of npm client, run this command:

  1. npm -v

Installing Angular CLI

  • Open Terminal/Command Prompt
  • To install Angular CLI, run the below command:
  1. npm install -g @angular/cli

installing angular CLI

· After executing the command, Angular CLI will get installed within some time. You can check it using the following command

  1. ng --version

Workspace Creation

Now as your Angular CLI is installed, you need to create a workspace to work upon your application. Methods for it are:

  • Using CLI
  • Using Visual Studio Code
1. Using CLI

To create a workspace:

  • Navigate to the desired directory where you want to create your workspace using cd command in the Terminal/Command prompt
  • Then in the directory write this command on your terminal and provide the name of the app which you want to create. In my case I have mentioned DataFlair:
  1. Ng new YourAppName

create angular workspace

  • After running this command, it will prompt you to select from various options about the CSS and other functionalities.

angular CSS options

  • To leave everything to default, simply press the Enter or the Return key.

angular setup

#angular tutorials #angular cli install #angular environment setup #angular version check #download angular #install angular #install angular cli

Clara  Gutmann

Clara Gutmann

1599459851

Angular Sass: How To Use Sass In Angular 9 Tutorial

Angular supports Sass, CSS, and Less to style global application styles as well as component styles. Angular components styles have an effective CSS encapsulation mechanism that assures any component CSS is local to the component and does not globally alter any styles.

Angular Sass Example

Why use Angular Sass? Well!! Sass (Syntactically Awesome Style Sheets) is an extension of CSS that allows you to use things like variables, nested rules, inline imports, and more. It also supports you to keep things organized and enables you to create style sheets faster.

In short,  Sass is a CSS preprocessor, which combines unique features such as variables, nested rules, and mixins (sometimes referred to as syntactic sugar) into regular CSS. The main object of Sass is to make the CSS coding process more comfortable and more efficient.

Sass is compatible with all versions of CSS. When working with the Angular CLI, the default stylesheets have the .css extension. We are using Angular CLI 8. So, if you have not used previously, then please upgrade your  CLI version. We will use the Bootstrap 4 Framework for this demo and see how we can configure the Sass in our Angular 9 application.

#angular #angular 9 #angular cli #css #angular sass

Benefits of Angular JS based Applications

AngularJS was introduced in the year 2009, by Google. AngularJS is a software framework used worldwide by developers. The entire base of this framework is open source. AngularJS has gained popularity among developers because of how it has become for them to create web applications. AngularJS helps in building apps that require less work and reduces the use of unnecessary codes. AngularJS application development is a javascript framework. AngularJS has a clear goal to make the entire process simpler, it also helps app development process and operations as much as it could. AngularJS is used for building applications that support MVC (model view controller) and SPAs (single page web apps) coding and programming structures. AngularJS has been used by some of the top companies in the world to simplify their app development process, like, Google, Paypal, Udemy, mobile site in iPad for HBO, etc. To read more click on the link.

#hire angular js developer #hire dedicated angular js developer #angular js application development #hire dedicated angular js team #hire best angular js application developer

Shawn  Durgan

Shawn Durgan

1595337366

Angular 9 CRUD by Example

Throughout this Angular 9 CRUD tutorial, we’ll be learning to implement CRUD operations by example using the latest Angular 9 version that has been released recently with Ivy support.

> ✋✋ Join our Facebook group 👈 to discuss anything related to Angular development.

We’ll make use of a CRUD REST API built using json-server which allows you to generate a full working REST API in no time.

What’s CRUD?

CRUD stands for Create, Read. Update and Delete — a set of operations often implemented in web apps to allow users to interact with a database.

In our tutorial, we’ll only focus on building the front-end using Angular 9, the back-end will be mocked using json-server.

We’ll not be learning how to use json-server but you can see the complete instructions from this tutorial after creating the Angular 9 project.

Angular 9 CRUD Tutorial Steps

  • Step 1 — Mocking the Backend Using json-server
  • Step 2 — Creating an Angular 9 Module
  • Step 3 — Importing Angular HttpClientModule and FormsModule
  • Step 4 — Creating Angular 9 Component(s)
  • Step 5 — Adding Angular 9 Routing
  • Step 6 — Creating an Angular 9 Service
  • Step 7 — Creating a Model
  • Step 8 — Implementing the CRUD Methods
  • Step 9 — Calling the CRUD Methods

#angular #angular-9 #angular 9 crud