Having trouble with integrating the Bootstrap Carousel in an Angular 2+ environment

I am currently working with Bootstrap 3.3.7 and Angular 4 for a project. My challenge lies in displaying multiple images in a Bootstrap Carousel, with the aim of showing three images per slide. However, I am encountering an issue where all the images are being displayed on a single page rather than within the Carousel setup. Below is a snippet of my code:

.html code:

<div class="container">
  <h2>Carousel Example</h2>  
  <div id="myCarousel" class="carousel slide" data-ride="carousel"  >
      <div class="col-sm-4" *ngFor="let data of  DataOne;let i = index" >
    <!-- Indicators -->
    <ol class="carousel-indicators">
      <li data-target="#myCarousel"  [attr.data-slide-to]="i" class="active"></li>
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner">
      <div class="item active">
        <img src="{{data.ImageUrl}}" alt="Los Angeles" style="width:50%;">
      </div>
    </div>
  </div>

    <!-- Left and right controls -->
    <a class="left carousel-control" href="#myCarousel" data-slide="prev">
      <span class="glyphicon glyphicon-chevron-left"></span>
      <span class="sr-only">Previous</span>
    </a>
    <a class="right carousel-control" href="#myCarousel" data-slide="next">
      <span class="glyphicon glyphicon-chevron-right"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>
</div> 

.ts code:

 export class TestComponent implements OnInit {

  DataOne:any=[
    {    
      "ImageUrl": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSW6QPbLZN-2Xs4Jb6DMZKivwTQtw5L3QH5AjJf4fPM8pubZato2Q"
    },
    {
      // additional image objects here
    }
  ];
  constructor() { }
  ngOnInit() {
  }
}

Here's an illustration of the current problem:

https://i.sstatic.net/50NTa.png

Answer №1

If you're using bootstrap carousel, each .item serves as the actual slider. To create a slider with 3 images, you need to place your .col-sm-4 and loop it three times inside the .item. Make sure to update your images object accordingly.

Check it out here:

Editor link: https://stackblitz.com/edit/angular-52pqo7

Data Json:

DataOne:any=[{
        "sliderImages": [{
                "ImageUrl": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSW6QPbLZN-2Xs4Jb6DMZKivwTQtw5L3QH5AjJf4fPM8pubZato2Q"
            },
            {
                "ImageUrl": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSW6QPbLZN-2Xs4Jb6DMZKivwTQtw5L3QH5AjJf4fPM8pubZato2Q"
            },
            {
                "ImageUrl": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSW6QPbLZN-2Xs4Jb6DMZKivwTQtw5L3QH5AjJf4fPM8pubZato2Q"
            }
        ]
    },
    {
        "sliderImages": [{
                "ImageUrl": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSW6QPbLZN-2Xs4Jb6DMZKivwTQtw5L3QH5AjJf4fPM8pubZato2Q"
            },
            {
                "ImageUrl": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSW6QPbLZN-2Xs4Jb6DMZKivwTQtw5L3QH5AjJf4fPM8pubZato2Q"
            },
            {
                "ImageUrl": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSW6QPbLZN-2Xs4Jb6DMZKivwTQtw5L3QH5AjJf4fPM8pubZato2Q"
            }
        ]
    }
]

Carousel code:

<div class="container">
  <h2>Carousel Example</h2>  
  <div id="myCarousel" class="carousel slide" data-ride="carousel"  >
      <!-- Wrapper for slides -->
      <div class="carousel-inner">
        <div class="item" 
          *ngFor="let data of  DataOne; first as isFirst;"
          [class.active]="(isFirst)"
        >
          <div class="col-sm-4" *ngFor="let slidImg of  data.sliderImages">
            <img src="{{slidImg.ImageUrl}}" alt="Los Angeles" style="width:50%;">
          </div><!-- .col-sm-4 -->
        </div><!-- .item -->
      </div><!-- .carousel-inner -->

      <!-- Indicators -->
      <ol class="carousel-indicators">
        <li 
          *ngFor="let data of  DataOne; index as i; first as isFirst;" 
          data-target="#myCarousel"  
          [attr.data-slide-to]="(i)"
          [class.active]="(isFirst)"></li>
      </ol>

    <!-- Left and right controls -->
    <a class="left carousel-control" href="#myCarousel" data-slide="prev">
      <span class="glyphicon glyphicon-chevron-left"></span>
      <span class="sr-only">Previous</span>
    </a>
    <a class="right carousel-control" href="#myCarousel" data-slide="next">
      <span class="glyphicon glyphicon-chevron-right"></span>
      <span class="sr-only">Next</span>
    </a>
  </div><!-- #myCarousel -->
</div><!-- .container -->

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the process for bringing a graphql file into typescript?

I'm having trouble importing a graphql file and encountering an error import typeDefs from "./schema/schema.graphql"; 10 import typeDefs from "./schema/schema.graphql"; ~~~~~~~~~~~~~~~~~~~~~~~~~ at cre ...

Creating columns in Bootstrap 4 with equal height content

Why is the height of each individual p tag within each column dictated by the length of the string of text and not by the column height, even though bootstrap 4 comes pre-loaded with equal height columns? I just want each client bubble to appear uniform wi ...

Center alignment within input-group-prepend using Bootstrap 4

Is there a way to create a fixed width input-group-prepend that is larger than its content, while still keeping the content centered horizontally? I've experimented with text-center and align-items-center, but the content always ends up left aligned. ...

Choosing the Angular5 model

I'm currently working with an Angular5 project that has a <select> element bound to an array of customers. Here's the code snippet: <select class="form-control" [ngModel]="record.customer_id" (ngModelChange)="setCustomer($event)" name=" ...

Error occurred during download or resource does not meet image format requirements

Encountering an issue when attempting to utilize the icon specified in the Manifest: http://localhost:3000/logo192.png (Download error or invalid image resource) from the console? import React from 'react'; import Logo from '../Images/logo1 ...

"Enhabling tablesorter pagination to ensure that buttons always stay in sync with

I am experiencing an issue with the pagination buttons staying at the bottom of my page, even when there are only 2 entries left on the last page. Check out my table here: Is there a way to make the pagination buttons dynamically move to the top based on ...

The inability to display a Stylesheet is due to the unsupported MIME type, even though the MIME type seems to be missing when checking for errors

Every time I try to apply a specific style to a particular page on a website (even though the same style functions properly on other parts of the web server), an error message pops up. The error notification states Refused to apply style from 'styl ...

Angular-highcharts used to create a stacked bar chart display

const dataArray = [{ name: "LTNS", id: 1, percentage: 60, price: 900000 }, { name: "NPCS", id: 2, percentage: 30, price: 342000 }, { name: "MARCOS", id: 3, percentage: 10, price: 600000 }] To create a stacked ...

What is the best way to define this.someProperty in a React component using TypeScript?

I'm encountering an issue with TS2339: Property 'someProperty' does not exist on type ''. I am attempting to add a new property to my React component using this.someProperty. interface MyComponentState { allClear: boo ...

Angular - Scope variable not updating after subscription

Having some trouble with a piece of code that subscribes from our service: Method getVideo() getVideo() { this.watchService.getVideoByVideoString(this.videoId).subscribe( (Response) => { this.videoData = Response; console.lo ...

What could be causing the inability to update a newly logged-in user without refreshing the page?

Hello, I have encountered an issue with my application that involves registration and login functionality. The problem arises when a new user logs in, as I must refresh the page to get the current user information. I am currently using interpolation on the ...

How do I set up a 5 column layout for the home page and switch to a 4 column layout for category pages in Magento?

Is it possible to display 5 columns of products on the Magento home page and only show 4 columns in the category area, or is this a pre-set parameter that applies platform-wide? ...

Merge two input fields into one to send data to the backend

I have created two input fields, one for selecting a date and the other for entering a time. Before submitting the form to the backend, I need to combine these two inputs into one variable. For example, <input type="text" name="myDate"> and <input ...

What is the method to retrieve a chat that has been ended with JavaScript SDK?

Whenever I attempt to retrieve a closed Twilio Conversation on the frontend using the JS SDK, I encounter a "Not found" error. Can closed conversations be fetched? My objective is to enable users to close a conversation while still having access to the m ...

What could be causing the error I'm encountering when attempting to declare a local variable?

I am new to TypeScript and am encountering an issue with variable declaration in my component function. I am trying to declare a local variable "contains" like this: setUpgrade($event) { contains : Boolean = this.selectedUpgrades.includes($even ...

Angular Deprecates Event before Unload Event

My goal is to prevent the user from navigating to any link if there are unsaved changes, and it works correctly in most cases but there are two exceptions: When the user clicks on "Log Out" When the user clicks on a toggle button at the layout level I at ...

Making changes to a property within a class does not automatically reflect those changes in the corresponding view

In my Typescript (.ts) file, this is the code I have: private today: Date = new Date(); And this is the corresponding HTML: <span [innerText]="today | date:dateFormat"></span> Everything displays perfectly, showing 22nd May. Now, I&apos ...

Troubleshooting: Angular 2 failing to load test component

Despite successfully getting my app.component to work in Angular 2, I am facing issues with a test one. It seems to be stuck at the "Loading..." message in my HTML. Can someone please take a look at my code and point out where I might have gone wrong? I fo ...

What could be causing the occasional appearance of a tiny glitch, like a pixel dot, below a menu item when utilizing this hover effect?

Occasionally, a tiny dot appears almost imperceptibly, like the one below the "prices" menu item: https://i.stack.imgur.com/kyaP3.png This is the hover effect code I created: :root { box-sizing: border-box; } *, *::before, *::after { box-sizing: ...

Menu options are unresponsive to clicks in the dropdown

Need help fixing my dropdown menu issue. Whenever I hover over the dropdown, it disappears before I can click on any items. Here is a snippet of the code causing the problem: #navContainer { margin: 0; padding: 0; padding-top: 17px; width: 220 ...