Navigating between active and inactive tabs using Bootstrap and Angularjs

My code uses Angularjs and Bootstrap tabs. Here is an example:

  <body ng-controller="TabsCtrl">
    <button ng-click="addNewWorkspace('jobs')">Open Tab jobs<i class="icon-plus-sign"></i></button> 
    <button ng-click="addNewWorkspace('invoices')">Open Tab invoices<i class="icon-plus-sign"></i></button> 
    <button ng-click="addNewWorkspace('payments')">Open Tab payments<i class="icon-plus-sign"></i></button> 

    <ul class="nav nav-tabs">
      <li ng-class="tabClass(tab)" ng-repeat="tab in tabs" tab="tab">
        <a href="{{tab.link}}" ng-click="setSelectedTab(tab)">{{tab.label}}
        <button ng-click="removeTab($index)">-<i class="fa fa-times"></i></button>
        </a>
      </li>
    </ul>
    <div ng-view></div>

  </body>

One issue I encountered is that when I open a new tab by clicking a button, the tab is created but not automatically active. I have to click on the tab to see its content.

Another problem arises when I close a tab after opening it. The closed tab does not retain the information from other open tabs. For instance, if I open the payments tab and then close it, the jobs info doesn't show up.

To address these problems, I attempted using $location.path and $window to manipulate the tab switching behavior, but without success.

I'm unsure if I misunderstood some crucial concepts related to tab functionality.

Check out the live demo with complete code here.

Answer №1

function addWorkspace(page) {
var newTab = {
    path: '#/' + page,
    title: page
};
workspaces.push(newTab);
currentTab = newTab;
goToLocation('/' + newTab.title);
};

Select the newly added tab as the current tab

currentTab = newTab;

Redirect to the new location

goToLocation('/' + newTab.title);

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

Why do we use ng-* instead of data-ng-* in Angular?

From my experience, I have found that data-ng-* is validation-friendly. However, I have noticed in several instances where snippets use ng-* instead of data-ng-*. This includes examples on the official Angular websites. What is the reason for this incons ...

Querying for Mongoose documents based on date ranges using the moment library

Below is the code for my current condition: if (req.query.df == "today") { start = moment().subtract(1,'days'); end = moment().add(1,'days'); condition = {"$gt":start, "$lt":end}; } else if(req.query.df = ...

Tips for storing JQUERY GET response (data) for later use in a separate function

In my database, there is a table that looks like this: +-----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+---------+-------------- ...

Is there a way to utilize a cookie in order for the website to recognize that I have already agreed to the terms?

It's common for websites to ask for cookie and privacy acceptance upon loading, especially in the EU. I'm looking for a way to automatically accept these cookies so I don't have to constantly click "accept all" every time I open Chrome. My ...

I am looking to understand how to execute basic PHP scripts using jQuery/AJAX

Due to the size of my actual example being too large, I have created a brief version: example1.php <!DOCTYPE html> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> <!--jQuery--> <body&g ...

Creating a sticky menu in a column using affix with a CSS bootstrap list-group

My goal is to develop a sticky menu utilizing CSS Bootstrap affix and the list-group menu. I have successfully implemented most of it, except for one issue when the user scrolls down. Upon scrolling down, the menu expands to fill the entire width of the ...

Class Chat Practice Application

I'm a beginner in the world of programming and I could really use some assistance with troubleshooting my ChatApp. My goal is to have my server send the message "Robot: (client's username) connected" whenever someone logs on. I've been gra ...

Updating the Vuex store with new information or setting data in an object

I'm currently struggling with adding or updating an object in the store using Vuex. import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({ state: { userData: {} }, mutations: { A ...

Save the Vue.js array into a SQL column as JSON, and ensure that any empty values are stored

After creating a form to save data in the "users" column of my "pack" table, I encountered an issue. The form data is stored in the "user" variable. When making an axios request, I push the data from the "user" variable into the empty array called "users. ...

How can I pass the color hex value from the controller to the view without using quotation marks?

I'm having trouble sending a value and color pair using JSON. The color value needs to be returned to the JavaScript in the view as color: "#FFFFFF". I can send it to the view that way, but once the browser reads it, it turns into color: &quot;#FF ...

Reducer is unaware of my current state within react redux

I am currently developing an application that allows users to add items to a collection. The process involves the user inputting the name of the collection item, and the corresponding item is added to the collection. However, I encountered an issue when di ...

What is the best way to transfer form data to another function without causing a page refresh?

Currently, I am in the process of developing a series of web applications using REACT JS. One specific app I am working on involves a modal that appears upon a state change and contains a form where users can input their name along with some related data. ...

What is the best way to compare the lengths of all nested child elements with each other?

I have an object with nested children, like this: $scope.artists.materials.items[] //contains list of items Now I need to compare the total length of each artist's list of items. If there is a mismatch in the number of items between artists, I want ...

Make a CSS div that expands based on the size of its absolutely positioned content

I'm currently experiencing some challenges trying to make a parent div expand to fit its contents which are positioned absolutely. The footer is reaching the bottom of the screen, but not the bottom of the document. This issue is clearly caused by th ...

Using jQuery to trigger alert only once variable has been updated

I have a question that may seem too basic, but I can't find the solution. How do I make sure that the variables are updated before triggering the alert? I've heard about using callbacks, but in this case, there are two functions and I'm not ...

Easily implement a "gentle" if statement check using JavaScript

When working in PHP, you can use the following code to check if a variable exists: if (@$some_var_exists) // do stuff How can you achieve a similar check in Javascript without encountering an error? Thank you UPDATE: I appreciate the responses. How ...

Struggling to modify a JSON value within a loop

I am currently developing a JavaScript code that involves updating one JSON based on conditions from another JSON. Below is the code I have written. const a = [{ "a": "Not Started" }, { "b": "Not Started" }, { "c": "Not Started" }, { "d": "Not S ...

Ray casting problem with THREE.js - Intersections can be quite fickle, often failing to return any intersecting objects

My current issue involves using the Raycaster in THREE.js to focus on objects with the mouse position. While I've successfully done this many times before, something seems to be off in my current setup. Strangely, the onFocus callback occasionally tr ...

Struggles with ajax and JavaScript arise when attempting to tally up grades

Currently, I am facing an issue with my JavaScript. The problem arises when trying to calculate marks for each correctly chosen answer based on information from the database. If an incorrect answer is selected, the marks are set to '0', otherwise ...

Utilizing an EllipseCurve for Extrusion Paths in Three.js - How is it Done?

The issue: I'm struggling to convert an EllipseCurve into a path that can be extruded along in Three.js. When I try to use the EllipseCurve as the extrude path, nothing shows on the screen even though there are no errors. However, if I switch it to a ...