Tips for setting a tab as active upon loading from a new page

I have multiple sub-menus on my Manage Profile page. The issue I am facing is that when navigating from a different page to a specific menu, such as Change Password or Payment Gateway, the default active tab is not being set properly. I need some guidance on how to resolve this.

Current Page:

<ul class="nav nav-pills nav-justified" >
    <li class="tablist" ng-class="tab === 1? 'active' : ''">
        <a ng-click="changeTab(1)" style="cursor: pointer;"><b>MANAGE PROFILE</b></a>
    </li>
    <li ng-class="tab === 2? 'active' : ''" style="border-left: 1px solid white; border-right: 1px solid white;">
        <a ng-click="changeTab(2)" style="cursor: pointer;"><b>CHANGE PASSWORD</b></a>
    </li>
    <li ng-class="tab === 3? 'active' : ''">
        <a ng-click="changeTab(3)" style="cursor: pointer;"><b>PAYMENT HISTORY</lt;b></a>
    </li>
</ul>

JS:

$scope.tab = 1;

$scope.changeTab = function(idx)
{
    $scope.tab = idx;
};

app.js:

 .when('/manageProfile', {
                controller: 'ManageProfileController',
                templateUrl: 'view/manageProfile.view.html',
                controllerAs: 'vm'
            })

Profile.html

<li><a href=#/manageProfile>ManageProfile</a></li>
<li><a href =#/manageProfile>ChangePassword</a></li>

How can I ensure that the Change Password tab is automatically set as active when selected in the profile page?

Answer №1

I completed this part of my project and would like to share my routes and controllers with you.

garageCtrl.js

class GarageController {
  /*@ngInject*/
 constructor($state,$stateParams) {
 this.$state = $state;
 this.$stateParams = $stateParams;

 this.filter = this.$stateParams.filter || 'all';

// Setting the initial tab based on the URL parameters
switch(this.filter) {
  case 'all': {
    this.activeTab = 0;
    break;
  }
  case 'commercial': {
    this.activeTab = 1;
    break;
  }
  case 'residential': {
    this.activeTab = 2;
    break;
  }
   default: null
  }
}

filterChanged(filter) {
 this.filter = filter;
 //SOME CODES FOR ASYNC//
 this.getGarageList(filter);
 this.pageChanged(this.page);
}

}

GarageController.$inject = [
  '$state',
  '$stateParams'
  ];
export default GarageController;

This snippet shows the view where I utilized a directive for the tabs using angular-bootstrap:

<uib-tabset active="$ctrl.activeTab" justified="true">
    <uib-tab index="0" heading="All"
      ng-click="$ctrl.filterChanged('all')">
    </uib-tab>
    <uib-tab index="1" heading="Commercial"
      ng-click="$ctrl.filterChanged('commercial')">
    </uib-tab>
    <uib-tab index="2" heading="Residential"
      ng-click="$ctrl.filterChanged('residential')">
    </uib-tab>
    <uib-tab index="3" heading="Valet"
      ng-click="$ctrl.filterChanged('valet')">
    </uib-tab>
  </uib-tabset>

Here is my routes.js, making use of angular-ui-router:

.state('dashboard.garage', {
  url: '/dashboard/garage?page&filter',
  views: { 
    '': { template: '<garage></garage>' },
    '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8be8e4e5ffeee5ffcbefeaf8e3e9e4eaebacec"><span class="__cf_email__" data-cfemail="6dfe959493819186a3958597">[email protected]</span></a>': { 
      templateUrl: 'src/app/templates/admin.dashboard.garage.template.html' 
    }
  },
  params: {
    page: {
      value: '1',
      squash: true
    },
    filter: {
      value: 'all',
      squash: true
    }
  }
})

Answer №3

Utilize the $routeParams service in AngularJS.

 var app = angular.module("myApp", ["ngRoute"]);
    app.config(function($routeProvider) {
        $routeProvider

       .when('/manageProfile', {
            controller: 'ManageProfileController',
            templateUrl: 'view/manageProfile.view.html',
            controllerAs: 'vm'
        })
        .when('/manageProfile/:tab', {
            controller: 'ManageProfileController',
            templateUrl: 'view/manageProfile.view.html',
            controllerAs: 'vm'
        })
       //:tab is used as a route parameter
    });


    app.controller("ManageProfileController",['$scope','$routeParams' ,function ($scope,$routeParams ) {
         $scope.tab = 1;
          if($routeParams.tab=="2" || $routeParams.tab=="3"){
            $scope.tab= Number($routeParams.tab) ; //retrieving the value of route parameter "tab"
         }
    }]);


<li><a href=#/manageProfile>Manage Profile</a></li>   //no Route parameter specified
<!-- or <li><a href=#/manageProfile/1>Manage Profile</a></li>--> // default tab set to 1
<li><a href=#/manageProfile/2>Change Password</a></li> // tab value set to 2

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

Ways to Design ASP.NET Buttons for Users with Disabilities

Recently, I attempted to style an asp button using CSS. The Button HTML: <asp:Button ID="btnClockin" runat="server" Text="Clock In" class="FullWidthButton" /> Here is the CSS code: .FullWidthButton {width:100%;} Everything was working fine until ...

What steps can be taken to adjust a module required in Node.js prior to initiating the build process?

Module A.js (standard NPM package): module.exports = { ... const wsUrl = `ws://${customHostname}:${customPort}/sockjs-node/websocket`; ... }; Module B.js: const a = require('A'); Is there a way to substitute ${customHostname} & ${cu ...

What could be causing the 404 error with my socket.io connection?

I'm currently in the process of recreating a basic chat application that was originally shared on the official website of socket.io. Within content.js, I have created the following module: var app = require('express'); var router = app.Ro ...

Is there a way to interact with a Bootstrap 5 dropdown in React without triggering it to close upon clicking?

I'm currently working on creating a slightly complex navigation bar using Bootstrap 5 and ReactJS. The issue I'm encountering involves the dropdown menu within the nav bar. Whenever I click inside the dropdown, even if it's just non-link te ...

Dynamic progress bar based on time remaining

Is it possible to create a progress bar that reflects the countdown timer displayed in a div? The timer counts down from 7 minutes and is shown as follows: <div id = "quiz-time-left"> 0:06:24 </ div> I want to ensure that if the page is reload ...

Ensuring consistent placement and scrollability of two divs across all screen sizes

I am in need of a solution to fix the top and bottom divs in the given image. The scroll should only occur when there is overflow. <!DOCTYPE html> <html> <head> <script src="//code.jquery.com/jquery-1.9.1.min.js"></script> ...

Is it possible to incorporate two ng-repeat directives within a single td element in a table?

The results so far Expected outcome Please advise me on how to incorporate two ng-repeats within one td. When I use a span tag afterwards, the expected result is not achieved. I have used one ng-repeat in the td and the other in a span tag, which is why t ...

The Bootstrap navigation bar fails to expand

After extensive searching, I still can't figure out why my bootstrap menu isn't opening as expected. I made sure to link JQuery first to try to resolve the issue, but it doesn't seem to have made any difference. I'm starting to wonder ...

What is the process to design and implement this button using HTML and CSS?

I am in need of creating a button similar to the google button shown in the image, but I am struggling to position it correctly on the margin. Can someone please assist me with this? The google button is the specific type of button that I am aiming to rep ...

Enhancing Blog Navigation with PicoCMS Pagination

After successfully setting up PicoCMS on my website, everything seems to be working well except for the blog pages. I am having issues with the "Older Posts" button not functioning as expected. Unfortunately, I couldn't find any information in the the ...

Collaborating on a Restangular resource across multiple controllers

After reading through this discussion, I have implemented the following approach using Restangular: app.factory('Account', function(Restangular) { var _account; return { get: function(id, success, failure) { // ...

Can you change the name of a key in the Firebase Realtime Database?

I have a question regarding updating the key value. How can I achieve this? Here is the data we are working with: https://i.sstatic.net/3VYSg.png Currently, I am using set() to write the data. I want users to be able to edit their bookTitle and have it ...

Data within object not recognized by TableCell Material UI element

I am currently facing an issue where the content of an object is not being displayed within the Material UI component TableCell. Interestingly, I have used the same approach with the Title component and it shows the content without any problems. function ...

Choose the text by clicking on a button

I currently have: <span class="description">Description 1</span> <button class="select">Select!</button> <span class="description">Description 2</span> <button class="select">Select!</button> <span clas ...

Recursive array filtering

My goal is to recursively filter my table using the key "excludeInMenu". I have successfully filtered the first table but am struggling with filtering the second content within "Items". Here is the code snippet I am currently using: {routes.filter(route = ...

Asynchronously retrieve the result from a previous NodeJS chain and forward it to a nested function for processing

When uploading an image from the client as Base64 to the server, I need to: Save the file to disk Get the result of the save (first chain) and pass it to the next chain Check the result in the next function using that(), if it's true, update the dat ...

The interval keeps resetting every time I want the initial one to expire first

I'm currently working on a battle system that utilizes intervals, and I've encountered an issue where it keeps refreshing instead of creating multiple intervals. When I stop pressing the button associated with it, everything goes back to normal. ...

Expanding columns to reach the full height of the page

Struggling to create three columns within the colmask and threecol div classes, excluding the header and footer. Any suggestions? Check out the website at I've attempted setting the body and html tags to 100% height. I've also experimented with ...

What steps should I take to repair my image modal button?

Within my application, I have an image modal placed inside a header tag, along with a label. There is some space between the label and the image modal. However, when the application is run, the page appears in a fixed position, but the label message does n ...

"Switching a div's visibility when a link is clicked

http://jsfiddle.net/FsCHJ/2/ Currently, when I add another link, it automatically uses the same functionality as the toggle button. What I want is for "Toggle Edit Mode" to toggle a hidden div on and off. I attempted to modify the code from $("a").click(f ...