Newly created item not showing up in Bootstrap carousel

Incorporating a bootstrap carousel with ng-repeat into my div has been quite the journey. One challenge I've encountered is adding new items to the current list and smoothly transitioning to that newly added item.

For instance, when I am at index 0 and click the add button, I expect to seamlessly slide to the new item in the carousel. However, despite setting the indexes correctly, it doesn't transition to the new item as desired.

$scope.items.splice($scope.currentIndex+1, 0, newItem);

$scope.currentIndex = $scope.currentIndex + 1;

$('#myCarousel').carousel($scope.currentIndex);

Instead of displaying the data from the new item, it still shows the content of item 1 until I manually click next to see the updated item. Even attempting

$('#myCarousel').carousel('next');
yields the same result.

Edit:

<div id="myCarousel" class="carousel slide" data-interval="false">
    <div class="carousel-inner">
        <div class="item" ng-class="{active:!$index}" ng-repeat="item in items">
            // rest of the html
        </div>
    </div>
</div>

Answer №1

Could you kindly provide the HTML section? Also, start with more than one slide to ensure at least three slides are present for debugging purposes.

In accordance with your code, I have crafted something similar below; please review and let me know if it meets your requirements:

<body ng-app="cApp" ng-controller="myslides">
<div id="myCarousel" class="carousel slide" data-interval="false">
<div class="carousel-inner">
    <div ng-repeat="item in items" class="item" ng-class="{active:$index==currentIndex}" >
                <div>{{item}}</div>
            </div>
    <h1>Hello Plunker!</h1>
</div>
<a class="left carousel-control" href="#myCarousel" ng-click="currentIndex=currentIndex!=0?currentIndex-1:items.length-1" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" ng-click="currentIndex=currentIndex<items.length-1?currentIndex+1:0" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="sr-only">Next</span>
</a>

</div>
<button class="btn btn-default" ng-click="addslide('another1')">add slide</button>
 </body>

Accompanied by the script below:

var app = angular.module('cApp', []);
app.controller('myslides', function($scope){

$scope.items = ['this is 1','this is 2'];

$scope.currentIndex = 0;

$scope.addslide=function(newItem){
$scope.items.splice($scope.currentIndex+1, 0, newItem);
$scope.currentIndex = $scope.currentIndex+1;

};
});

If you intend to utilize Angular, I personally recommend utilizing angularui which offers a straightforward carousel with pre-built functions while allowing for customization.

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

How can I configure Express to act as a pass-through proxy server?

How can I set up an express server to act as a proxy? Requirements: Support for both http and https Ability to function behind a corporate proxy Option to provide custom content for specific URLs I have experimented with various modules such as http-pr ...

Trouble with pre-selecting an option in a select dropdown

I am having an issue with my select element in AngularJS. When the page loads, I am getting empty options in my select dropdown. To fix this, I tried preselecting the first option in the select but it didn't work as expected and I encountered a ' ...

Swapping out a code snippet within an HTML document using JavaScript

As a new member of this community, I present to you my very first problem that needs solving. It might be a piece of cake for some of you, but for me, it's proving to be quite tricky. The task at hand involves replacing or removing a string to make ev ...

Differences between JavaScript's window.onload and body.onload functionsWhen

My inquiry is similar yet slightly distinct from the one queried here: window.onload vs <body onload=""/> The comparison in that prior query was between utilizing window.onload and inline js. My question pertains to the disparity between ...

trigger form submission and refresh on React JSON Schema Form

I'm currently utilizing react-jsonschema-form to create a form, but I'm facing an issue where the function linked to the onSubmit button isn't executing. It's puzzling why clicking the button causes the page to reload without running th ...

Having trouble converting the JQuery result from the REST request into the correct format

Currently, I am working on making a REST request for an array of objects using JQuery. During the execution of the code within the "success" section, everything works perfectly fine - the objects in the array are converted to the correct type. However, I ...

An issue has been encountered during the process of disconnecting with mongoose

I'm new to mongodb/mongoose and javascript. When I try to use mongoose.disconnect, it throws an error return new error_1.MongoNetworkError('connection establishment was cancelled'); ^ MongoNetworkError: connection establishment was cancell ...

Having trouble initiating the server using npm start

Just starting out with nodeJs: I have created a server.js file and installed nodejs. However, when I try to run "npm start", I encounter the following errors. C:\Users\server\server.js:43 if (!(req.headers &amp;& req.headers. ...

Patience is key for a fully completed JSON in JavaScript

I recently came across a similar discussion on Stack Overflow, but it involved using JQuery which I'm not using. My issue is that I need to ensure my JSON data is fully loaded before calling my function. I understand the concept of using a callback, ...

Strategies for extracting methods and refactoring to a separate file for better reusability

Still relatively new to the JQuery/javascript realm, I've put together a functional jqgrid with a datepicker and custom control (using jquery auto complete) by piecing together code samples I came across online. This code has been added to a T4 templa ...

Retrieve the JSON object's name and loop through its child objects

Just diving into JavaScript and web development, seeking some guidance. I'm currently working on gathering information to format and trigger another request. Here's what I have so far: JavaScript $( document ).ready(function(sb_shows) { $. ...

Having trouble retrieving the DD-MM-YYYY date format in Vue.js from MySQL database

Hi, I am struggling with setting the format of dateCreated (the date when the account was created) to DD-MM-YYYY. In my Vue page, the code looks like this: https://i.sstatic.net/8SSAE.png The issue is that the website displays the date in the format sho ...

The Catcomplete widget malfunctioned after the update of jQuery UI from version 1.8 to 1.12

I've encountered an issue with my code after updating to jQuery UI 1.12 var currentCategory = ""; $.widget("custom.catcomplete", $.ui.autocomplete, { _renderMenu: function (ul, items) { var self = this; $.each( ...

JavaScript: Can you clarify the value of this variable using five sets of double quotations?

Could you please review the code snippet below for me? <script type="text/javascript"> function recentpostslist(json) { document.write('<ul class="recommended">'); var i; var j; for (i = 0; i < json.feed.entry.length; i++) { ...

Error with Angular TicTacToe Project: Out of Range

Currently I'm working on an Angular TicTacToe project using Angular version 17.0.4. I encountered a Range Error stating "Maximum call stack size exceeded." In order to resolve this issue, I replaced all instances of AppComponent with SquareComponent. ...

To achieve proper display of multiple boxes, it is essential for each box to be

My current approach involves adding boxes to the scene based on specific dimensions for height, width, and depth, and it works perfectly when the boxes are all square. https://i.sstatic.net/HdDSX.png However, the issue arises when I try to use a rectangu ...

Canceling prior requests when a new request is initiated in AngularJS

I have been working on implementing an autocomplete feature that should abort previous requests when users enter new characters, but I am facing issues with it. Below is my controller code: $rootScope.autocompletePendingRequests = []; $rootScope.autoComple ...

The communication between ReactJS using Axios and a Go API

I currently have a ReactJS application running on a NodeJS server with Express. I chose this setup in order to synchronize the routers between my frontend and backend. Additionally, I am using a Go API with Chi that performs well (I have tested it with Pos ...

Consistently Encountering The 404 Error

Greetings! Below is the code snippet from my app.js: var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieParser = require(& ...

What is the best approach for $evaluating an attribute within a directive that is being utilized in conjunction with ng-repeat

I need help with a directive that isn't working as expected. The directive is simple and doesn't have an isolate scope. It's used within an ng-repeat element, but I want to restrict its functionality to specific elements within the ng-repeat ...