Setting tabs dynamically in AngularUI Bootstrap (ui.bootstrap.tabs) according to changing data

I have been experimenting with the tabs feature of AngularUI Bootstrap by creating tabs dynamically.

<div ng-controller="TabsDemoCtrl">
<uib-tabset active="activeForm">
  <uib-tab  index="$index" ng-repeat="tab in tabs" >
    <uib-tab-heading>{{tab.title}}</uib-tab-heading>
    {{tab.content}}
  </uib-tab>
</uib-tabset>

Although I am setting an active tab in the controller, when the tabs array is modified, the UI Tabs update but the active tab remains unchanged.

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ngSanitize', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('TabsDemoCtrl', function ($scope, $window) {
  $scope.tabs = [
    { title:'Dynamic Title 1', content:'Dynamic content 1' },
    { title:'Dynamic Title 2', content:'Dynamic content 2' },
    { title:'Dynamic Title 3', content:'Dynamic content 3' },
    { title:'Dynamic Title 4', content:'Dynamic content 4' }
  ];

  $scope.changeTabs = function(){
         $scope.tabs = [
       { title:'Dynamic Title 5', content:'Dynamic content 5' },
       { title:'Dynamic Title 6', content:'Dynamic content 6' }
     ];
     $scope.activeForm = 0; //Not working, how can I select tab dynamically?
  };

  $scope.model = {
    name: 'Tabs'
  };
});

Any insights on what could be causing this issue?

Check out the Plunker example here: https://plnkr.co/edit/Ow7Cd1eidCgaLNOhX1Vl

Thank you for your help. Paul

Answer №2

Hello, I have a piece of code that requires modification:

$timeout(function(){
 $scope.activeForm = 0; //The current implementation is not functioning as expected. How can I dynamically select the tab?
},0);

By triggering the digest cycle, your code should start working correctly.

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

Error encountered in jQuery validation script due to incorrect data type

Looking to experiment with the jQuery validate plugin in an MVC application by trying a simple example. Here is a JS module with a method: ValidateRestriction: function (element) { var inputs = $('form').validator(); inputs.data("validat ...

React Hook Form is experiencing an excessive amount of re-renders which can lead to an infinite loop. React sets a limit on the number

Currently, I am working on displaying a field named party. Once this field is selected, a list of products should be rendered. In my project, I am using React Hook Form along with the watch hook to keep track of changes. <FormProvider {...methods}> ...

Getting AJAX data in PHP: A comprehensive guide

As a student, I am working on creating a simple form for my website that will utilize ajax to save the submitted details to the database using PHP. I prefer not to use any external libraries like jQuery. Although I have successfully implemented the ajax "g ...

How can I read "binary" characters from a file using JavaScript?

When I mention binary, I'm referring to ghjkl54╞←‼╝454┴ rather than 10101110. I am interested in implementing a tilemap loading functionality in JavaScript without having to redo my map editor, which is coded in Java and exports maps as bin ...

Uncovering components generated by React JS with BeautifulSoup

My goal is to extract anchor links with the class "_1UoZlX" from the search results on a specific page - After analyzing the page, I realized that the search results are dynamically generated by React JS and not readily available in the page source or HTM ...

Trouble with updating the view when an array is modified in ng-repeat? It seems like using $scope.$apply() may not

When updating the array inside a function, the view does not automatically update. However, if you use console.log to check the array after pushing values, it shows the updated array. Even trying $scope.apply() inside $timeout did not solve this issue. Ja ...

Tips on how to make the cursor blink in an input text box after tab switching

Whenever I click the button, the cursor in the input field blinks. However, the issue arises when I switch to another tab and return to this tab - the cursor is no longer in position and I have to click the input field again. Even after switching tabs an ...

The error message "Uncaught ReferenceError: require is not defined" is commonly encountered when using Webpack

Despite countless similar questions, none have provided a solution to my issue because the underlying problem seems to elude me. I am attempting to bundle files for the browser using webpack 4.26.1, but no matter what I try, I consistently encounter the er ...

Fill Datalist with Database Information

As a beginner in javascript and php, I am trying to populate a datalist from a mysql table. However, I am facing some difficulties with the relevant codes provided below. Although I can fetch data from the database, I am struggling to display them in the d ...

Detecting iFrame Scroll Events

I have been scouring the internet and experimenting with various methods, but I am struggling to detect scrolling of an iframe. The iframe source is located on the same server in the same directory as the page. Below is the initial code I am working with: ...

React Big Calendar - Creating a personalized property for a unique perspective

My React Big Calendar library has a custom view for the year. <Calendar localizer={localizer} events={events || []} startAccessor="start" endAccessor="end" defaultView="year" views={{ year: YearView }} c ...

Why is my Angular router displaying the page twice in the browser window?

Angular was initially loading the page on the default port localhost:4200. I wanted it to serve as localhost:4200/specialtyquestions when the app builds, and that is working, but the pages are appearing twice in the browser. Any ideas on what might have be ...

Tips on Including Static Header and Footer in Multiple HTML Pages

What is the best way to reuse a header and footer on multiple HTML pages without repeating code? I have 5 HTML pages that all need to include the same header and footer elements. How can I efficiently reuse these components across all pages? ...

Using a function to identify and check dynamically added checkboxes

I am using a PHP page that loads a group of categories from another PHP script as checkboxes. Here is the format for the checkboxes: <input type='checkbox' class='cat-selector' id='Business' data-toggle='checkbox&apo ...

Unable to trigger JQuery .blur event

I am currently working on implementing server-side validation for an input field that needs to be validated when it loses focus. However, I'm running into an issue where the alert is not triggered when the input field loses focus. Here's a snipp ...

The issue with Three JS is that concentrating on the avatar's perspective while it moves is not functioning properly

I am currently delving into the world of Three JS as a novice in 3D programming. Following a tutorial from the book , I am working on a game where an avatar navigates through trees. My current challenge involves getting the camera to continuously focus on ...

Experimenting with angular provider using the $http success callback

Updated to Incorporate Testing: I am working on an Angular application with a controller setup like this: angular.module('myModule') .controller('myController', ['$scope', 'myProvider', function($scope, myprovid ...

Alert received following installation of angular-ui-router

Upon installing angular-ui-router via npm install, the following message appeared: WARNING! The npm package "angular-ui-router" has been rebranded as "@uirouter/angularjs". Please update your package.json file. See https://ui-router.github.io/blog/uiroute ...

The problem I am facing is with the React Ant d Modal Component, where I am unable to render the

Hey there! I'm currently working with React Js and the 'Ant design Modal Component'. When I click on a button, the modal should open and display the content of each user with their unique ID. However, I'm facing an issue where all modal ...

Managing various conditions in React is crucial for proper functionality

I am facing an issue with the if statement related to creating conditions based on the number of objects in an array. Specifically, I need the condition to be true if there are 2 objects in the array and false if there is only 1 object. These objects are s ...