Learn the process of activating or deactivating tabs on an AngularJS frontend

My webpage has three tabs that I'm struggling to enable and disable correctly. Whenever I click on a tab, it routes me to the desired page but the tab remains active even though it should deactivate. Strangely, removing the code 'window.location.assign(view);' fixes this issue.

As a newbie to AngularJS/CSS, I need urgent help in resolving this. Thank you in advance!

Below is the code snippet from my HTML page and controller:

HTML-

<ul class="nav nav-pills nav-stacked" > 
    <li  ng-class="{ active: isSet(1) }" style="width:130px;display:inline-block">
         <a   ng-click="setTab(1,'#/Employers')" style="background-color:#0072AD" ><b style="color:white">EMPLOYERS</b></a>                    
    </li>                                   
    <li ng-class="{ active: isSet(2) }"  style="width:110px;display:inline-block">   
        <a ng-click="setTab(2,'#/Products')"  style="background-color:#0072AD"><b style="color:white">PRODUCTS</b></a>
     </li>
      <li ng-class="{ active: isSet(3) }"  style="width:180px;display:inline-block">
         <a  ng-click="setTab(3,'#/ControlReports')"   style="background-color:#0072AD"><b style="color:white">CONTROL REPORTS</b></a>                                   
      </li>
</ul>

Controller:

'use strict';

angular.module('Business.Header', [])
    .controller('HeaderCtrl', ['$scope','$location','$rootScope',
function ($scope, $location, $rootScope) {

    $scope.tab = 1;    

    $scope.isSet = function (tabNum) {

        return $scope.tab === tabNum;

    };

    $scope.setTab = function (newTab, view) {

        $scope.tab = newTab;
        
        window.location.assign(view);

    };

}]);

CSS:


/* Text Recolor */
h1, p, a {
  color: #4DC9C9 !important;

}

/* Button Recolor */
.nav-pills > li.active > a, .btn-primary {
  background-color: #6C6C6C !important;  
  border-color: #6C6C6C !important;
}

Answer №1

Consider using ng-class without the function like shown below:

<ul class="nav nav-pills nav-stacked" > 
    <li  ng-class="{ active: tab === 1 }" style="width:130px;display:inline-block">
         <a ng-click="setTab(1,'#/Employers')" style="background-color:#0072AD"><b style="color:white">EMPLOYERS</b></a>                    
    </li>                                   
    <li ng-class="{ active: tab === 2 }" style="width:110px;display:inline-block">   
        <a ng-click="setTab(2,'#/Products')" style="background-color:#0072AD"><b style="color:white">PRODUCTS</b></a>
    </li>
    <li ng-class="{ active: tab === 3 }" style="width:180px;display:inline-block">
         <a ng-click="setTab(3,'#/ControlReports')" style="background-color:#0072AD"><b style="color:white">CONTROL REPORTS</b></a>                                   
    </li>
</ul>

If this method doesn't work, you may want to try this approach:

$scope.setTab = function (newTab, view) {
    $scope.tab = newTab;
    window.location.assign(view);
    if($scope.$$phase) $scope.$apply();  // force update after assignment
};

Alternatively, you could use:

$scope.setTab = function (newTab, view) {
    $scope.tab = newTab;
    if($scope.$$phase) $scope.$apply();  // force update before assignment
    window.location.assign(view);
};

Answer №2

Make sure to enclose the class name in single quotes - 'active'

For example:

<ul class="nav nav-pills nav-stacked" > 
    <li  ng-class="{ 'active': tab == 1 }" style="width:130px;display:inline-block">
         <a   ng-click="setTab(1,'#/Employers')" style="background-color:#0072AD" ><b style="color:white">EMPLOYERS</b></a>                    
    </li>                                   
    <li ng-class="{ 'active': tab == 2 }"  style="width:110px;display:inline-block">   
        <a ng-click="setTab(2,'#/Products')"  style="background-color:#0072AD"><b style="color:white">PRODUCTS</b></a>
     </li>
      <li ng-class="{ 'active': tab == 3 }"  style="width:180px;display:inline-block">
         <a  ng-click="setTab(3,'#/ControlReports')"   style="background-color:#0072AD"><b style="color:white">CONTROL REPORTS</b></a>                                   
      </li>
</ul>

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 Faux Font-Icon Effect Using CSS and PNG

I am looking for a way to change the color of a transparent, grey-scale PNG icon as easily as changing the color of a font icon. More details: The PNG icons have both outer transparencies and inner white areas for the actual symbol, along with a slight gr ...

Stop the padding of list items from wrapping when displayed in a horizontal list

My goal is to create a horizontal unordered list (<ul>) with four columns in each row. The number of list items (<li>) may vary and change during runtime. Existing Code and Outcome ul { -webkit-columns: 4; /* Chrome, Safari, Opera */ -m ...

Navigating a table list in React using arrow keys without inadvertently scrolling the scrollbar

Currently, I've created a table component that contains a list of items. I've implemented hotkeys using the react-hotkeys package to allow users to navigate through the list using the 'arrow up' and 'arrow down' keys. The issu ...

Having trouble locating the CSS file when trying to replace bootstrap with Sass?

I'm running into an issue with customizing my bootstrap theme using Sass. After installing Sass and creating both main.scss and main.css files, I imported bootstrap in my main.scss file as per the documentation. @import "node_modules/bootstrap/scss/b ...

Is it feasible to create a doughnut chart with curved edges?

My goal is to create a doughnut chart, but my search for reliable CSS/SVG/Canvas solutions has not been successful. https://i.sstatic.net/Rq6Lx.jpg I want each segment to have fully rounded corners, which presents a unique challenge. ...

Validating input fields in an AngularJS form without specifying a name attribute

Is there a way to monitor input field validation within a form without relying on the field name or using an array like user[user_name]? Here is an example of what I am trying to accomplish: <div ng-class="{ 'has-error' : saveUserForm.user[f ...

Ways to use jQuery to disable row form elements in the second and third columns

I need a way to deactivate form elements in the second and third columns, starting from the second row until the nth row using a jQuery selector. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> ...

Uncovering Secret Information with Beautiful Soup 4

I've hit a roadblock with my current project. I'm attempting to extract player names and projections from this website: The plan involves running a script that loops through various PID values, but that part is not causing any issues. The real c ...

The issue of memory leaks in Angular DataTables Directive arises specifically when utilizing the $compile

I have encountered a significant memory leak while utilizing a custom DataTables directive that relies on jQuery's DataTables plugin. The memory leak seems to be stemming from a particular function within the directive. var rowCompiler = function(nRo ...

Creating a debounce functionality by isolating it from a keydown event listener

I have a table with selectable rows using up and down arrows. When a row is selected, an ajax call is made to fetch the record. To prevent users from spamming ajax requests, I implemented a debounce function called from a directive triggered by a keydown ...

Why won't the display: block CSS style apply to my div element?

Take a look at my code: https://jsfiddle.net/r62p4209/ I created a search bar with company brand name and some buttons on the right, positioned in the center. My aim is for the search bar (from "search by:" to the "Go!" button) to move onto the next lin ...

Export the Excel file with hyperlinks to HTML format so that they can be easily opened in a file explorer instead of a web browser

I've got an excel spreadsheet that contains links to local folders. In order to optimize its compatibility with various devices, I convert it to HTML format. Everything seems to be working smoothly, except for one minor issue - when clicking on the li ...

Tips for sending parameters upon clicking a bootstrap button/link

I need assistance with my webpage that utilizes the Flask/Python framework and includes a Bootstrap button. I want to pass values or parameters when the button/link is clicked. How can this be achieved? In the code snippet below, Generatebill represents t ...

Instructions on utilizing the CSSStyleSheet.insertRule() method for modifying a :root attribute

Is it possible to dynamically set the background color of the :root CSS property in an HTML file based on a hash present in the URL? The code provided does change the background color, but unfortunately, the hash value doesn't persist as users navigat ...

Tips for ensuring Node.js waits for a response from a substantial request

When uploading a large number of files, the process can take several minutes. I am currently using a multi-part form to post the files and then waiting for a response from the POST request. However, I am facing issues with timeouts and re-posting of files ...

Create an unordered Vue component object

In the data retrieved from the backend, there is an object containing various properties, one of which is the 'average' value. This object is ordered based on that value and when accessed through Postman, it appears as follows: ranking: ...

Creating visually appealing definition lists

My goal is to customize the appearance of a shopping cart that is created by a rigid system where I cannot modify the html code. The categories list is generated in the following manner: <dl id='dlCatLvl1' class='clsCatLvl1 clsOffCat1&ap ...

Sending information to Modal in AngularJS

When trying to pass data from a GET request to a Modal window, I often find myself using "item" for "response.data.item"... $http.get(link).then(function (response) { var modalInstance = $uibModal.open({ templateUrl: 'newsModal.html&apo ...

Trouble Viewing Image File

I am facing an issue where I cannot upload an image file from my computer onto my HTML file, as it is not showing up in the browser. However, when I link an image file from the web, it works perfectly fine. I have double-checked the file path and ensured ...

Steps for identifying the file format of a document with JavaScript

Can someone assist me in determining a file type using JavaScript within the context of this HTML code? <div class="indi-download"> <div class="pull-left"> <h6 class="file-format">%file_display_name%</h6> </div> <div c ...