Switching classes on the currently active tab

I have implemented Angular UI tabs with buttons that switch to different tabs. I am attempting to change the style of a button when its corresponding tab is active by using ng-class to dynamically apply styles.

Inside my scope, I have a function that returns the active tab:

 $scope.getActive = function() {
     return tabSelector.active;
    console.log("active tab = " + tabSelector.active);

 }

This function returns a number (0, 1, 2 or 3) depending on which tab is currently active.

Below are my CSS classes for the buttons:

.btn-white-hollow {
    padding: 20px 20px 20px 20px;
    margin-top: 16px;
    background-color: #fff;
    color: #0A135E;
    font-weight: bolder;
    -webkit-transition: all linear .2s;
    transition: all linear .2s;
}

.btn-white-hollow-selected {
    padding: 20px 20px 20px 20px;
    margin-top: 16px;
    background-color: #fff;
    color: #fd7400;
    border-bottom: 2px solid #fd7400;
    background: #fff;
}

I attempted to use ng-class in the following format, but it did not work as expected:

<li class="seekerhead" >
    <button type="button" ng-class="{ 'btn-white-hollow-selected' : getActive() == '0', 'btn-white-hollow' : getActive() !== '0' }">Ask a Question</button>
</li>

The issue is that ng-class is not applying the correct CSS classes. I also tried using getActive() !== 0, but that approach didn't work either. How can I resolve this and make it functional?

Answer №1

Although the provided code may lack some information, you can still achieve your desired outcome by simply following the instructions. In your controller, define a variable named 'activeTab' within the scope and set it to the panel number that should be active initially.

$scope.activeTab = 1; //assuming you want the first tab to be active

Next, in the HTML code for your tabs, include the following snippet:


<button type="button" ng-class="{ 'btn-white-hollow-selected' : activeTab === 1, 'btn-white-hollow' : activeTab !== 1 }" ng-click="activeTab = 1">
    Ask a Question
</button>

This assumes that the button mentioned is associated with the respective tab.

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

Creating a table using Ng-repeat in AngularJS: A Step-by-Step Guide

I'm trying to figure out how to create the table below using ng-repeat. Unfortunately, I don't have permission to modify the json structure so I need to work with it as is. Here's my json: $scope.carCollection = { 'Toyota': [ ...

Is the sliding navigation glitching due to the video background?

Currently, I am in the process of developing a website with a captivating full-page video background. The nav menu gracefully slides out from the left side using jQuery UI when users scroll down to view the site's content. To view the work-in-progres ...

Navigate through the contents of a table featuring intricate colspan and rowspan elements, while keeping both headers and left columns in

I am facing a challenge with a dynamically generated table that has complex structure including colspans and rowspans. My goal is to fix the headers and, if possible, even lock the first few rows on the left side while allowing the content of the table to ...

What is the best way to empty an input field after adding an item to an array?

In my Angular 2 example, I have created a simple functionality where users can add items to an array. When the user types something and clicks the button, the input is added to the array and displayed in a list. I am currently encountering two issues: 1 ...

PHPDocumentator mistakenly generated .cs files instead of .css files

After installing PHPDocumentator, I encountered a minor issue. It seems to be generating CSS files with the extension ".cs" instead of ".css". This discrepancy is causing problems since the HTML files reference these stylesheets as "style.css", "print.css" ...

Ensure that a span within a cell table has a height of 100%

Is there a way to have the span element expand to full height? Click here <table class="table table-condensed table-hover table-striped"> <thead> <tr> <th class="shrink"></th> <th class ...

Utilize URL parameters in Node.js and Express for effective communication

I have an endpoint ..com When I perform a curl call, the endpoint looks like this: ..com?param1=true Now, I am attempting to make a similar call from Node.js. However, I'm uncertain about whether param1 should be included in the headers, concatenate ...

A component with angular features will not persist

I have a menu component: <nav class="mxmls-mobile-nav"> <button class="mobile-menu-btn visible-xs visible-sm " ng-click="asideVm.open = false"> <i class="ion-android-close"></i> </button> </nav> <a class ...

Challenge with the positioning of HTML output layout

Is there a way to center the output/result of the HTML code below both horizontally and vertically on the web page using CSS? I have tried the following code but it only centers the output horizontally. Vertical alignment is not working properly. Can someo ...

Printing CSS in ASP.NET without a print dialog box or a pop-up confirmation message after successful printing

My goal is to create a list of greeting cards, each printed to a specific size (A4 folded down the longer edge), using HTML and CSS. I want the cards to be rotated 90° on the page. Ideally, I'd like to print the list without viewing the HTML and bypa ...

Distinguishing Design with CSS3

Here is the code snippet I am using: <table width="562" cellspacing="0" cellpadding="0" border="0" align="center" class="genericClass"> (nested tags are here) </table> These are the related styles in my stylesheet: table.genericClass a ...

What is the best way to center text within a div that is wider than 100%?

On my webpage, I have a header set to 100% width and text positioned on the banner. However, when zooming in or out, the text's position changes. How can I ensure that the text stays in place regardless of zoom level? Example: jsfiddle.net/5ASJv/ HT ...

Searching for dates in an ng-repeat using AngularJS

I've implemented a global filter for my array of data, and it seems to be working fine overall except for the dates. I've done some research but couldn't find a clear solution. I've come across custom filters, but I'm wondering if ...

The div needs to be positioned above another div, not beneath it

Just returning to the world of coding and struggling with a basic problem. Any help would be greatly appreciated. I am trying to position the 'header-top' (red) div at the top, and the 'header-bottom' (blue) div at the bottom. However, ...

Using ng-style to apply a background image with a dynamic id

Hey there, I'm facing an issue here. The link provided below seems to not be working properly. Even though the id is set correctly within the scope, it seems like there might be a parsing error occurring. Any thoughts on what might be causing this pro ...

Excessive background image size causing issues on iPad 4

Upon launching the website on an iPad4 in portrait mode, I noticed that the background image is too high, matching the height of the section set to 100vh. Is there a way to adjust the background image to be shorter? Here is the link to the website for re ...

Choosing the Right Server Software for AngularJS Web Development

I'm seeking clarification on a particular concept: After being away from web development for around 3-4 years, I'm feeling lost as I try to get back into it and catch up with the latest technologies. In the past, I was accustomed to using JSP/P ...

What's the best approach when it comes to declaring CSS in an HTML document: should you

I have a quick question about HTML and CSS. Recently, I started using Twitter's Bootstrap framework and added this code snippet to the head of my index.html file: <link href="css/bootstrap.css" rel="stylesheet"> <link href="css/style.css" re ...

Remove the boundaries from the grid and only retain the box

I am not skilled in css, but I simply want to eliminate the interior edges of the box and retain only the outer edges. .grid-container { display: grid; grid-template-columns: auto auto auto; background-color: #2196F3; padding: 10px; } .grid-ite ...

An issue arose while trying to create the perfect seed with yeoman

While working on my first Yeoman webapp using the ultimate-seed-generator, I encountered some errors that are hindering progress: C:\Users\Fidel\Desktop\Nueva carpeta\new-proyect>npm install > <a href="/cdn-cgi/l/email ...