Guide: Highlighting a selected link in Navigation without redirecting the page can be achieved in AngularJS by utilizing

I have a list of links that I want to be able to highlight when clicked without redirecting to the page. Below is the code snippet:

HTML

<ul class="nav nav-list">
   <li ng-repeat="navLink in navLinks" ng-class="navClass('{{navLink.Title}}')">
      <a href='#/{{navLink.Title}}'>{{navLink.LinkText}}</a>
   </li>
</ul>

Javascript:

 app.controller('CreatorController', function($scope,$rootScope,$location,$http,CreatorService,FlashService) {
$scope.navLinks = [{
        Title: 'home',
        LinkText: 'Home',
    }, {
        Title: 'about',
        LinkText: 'About Us'
    }, {
        Title: 'contact',
        LinkText: 'Contact Us'
    }];

    $scope.navClass = function (page) {
        var currentRoute = $location.path().substring(1) || 'home';
        return page === currentRoute ? 'active' : '';
    };
});

The current code highlights the link when clicked but also redirects to that particular page. How can I modify the code so that it only highlights the selected link without redirecting to the page upon clicking?

`

Answer №1

If you need to make sure a link is only activated by double clicking, you can utilize the ng-dblclick attribute. This will ensure that the link is highlighted on the first click, but will not redirect until it is double-clicked.

To set up a function in your controller that handles the redirection, you can create a function like "redirectPage". Then, you can implement the link as follows:

<a ng-dblclick="redirectPage()">link</a>

For a demonstration of how ng-dblclick functions, you can visit this JSFiddle.

Hopefully this solution meets your needs :)

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

A guide on implementing the grid feature in Angular.js

Looking to add the following features in my Angular grid: Paging Filtering Column-wise sorting Can anyone recommend an Angular JS grid that supports these functionalities? ...

What is the best way to horizontally center a div with the layout attribute set to "row"?

I am currently working with a grid composed of cards from the Angular Material library. The issue I am facing is that the cards have a fixed size, which results in extra space on the right immediately after wrapping. My goal is to eliminate this space by ...

Filtering in AngularJS seems to only work in one direction

I am working on implementing two different ways of filtering data - one by clicking on letters and the other by typing in an input field. <body ng-controller="MainController"> <ul search-list=".letter" model="search"> <li class= ...

Why have the bars been positioned on the left instead of the right, and why does the mobile version require sliding instead of simply accessing the menu through a function?

Hello everyone, I'm currently working on designing a mobile-friendly header menu with the bars positioned on the right side of the screen. The goal is to utilize JavaScript to allow users to click either an 'X' icon or the bars to open the m ...

Updating the color with the help of useState in React, with the use of Material

I enjoy changing the color of the icon with a click using a useState hook. I have added a click handler to the icon for this purpose. Below is the code snippet: import React, { useState } from 'react'; import './App.css'; import ThumbU ...

Is there a way to enable code completion for Firebase on VS Code?

After successfully setting up Typescript for code completion following the guidelines provided in this resource, I now want to enable code completion for Firebase in VS Code. However, I am unsure of the steps to achieve this. How can I activate code compl ...

Utilize JavaScript and HTML to show error messages based on whether the user is deactivated or if the input is invalid

I need to show different error messages under the 'Email Address' input if the user is disabled and under the 'Password' input if the user is invalid. In my debugging process, I discovered that a user being disabled results in a "Status ...

Encrypting href links in Nodemailer with Handlebars for forwarding purposes

I am working on a project that involves utilizing NodeMailer + Handlebars for sending and tracking emails. I am interested in changing every href link to the project URL before redirecting to the destination link. For example: Original email: <a href ...

Mastering the art of completing a form using AJAX

I'm working on a checkbox that triggers AJAX to create a new log. It populates the necessary information and automatically clicks the "create" button. However, I'm facing an issue where the hour value is not changing. Any help on what I might be ...

Creating child rows with forms in Datatables

Currently, I am utilizing this particular example where I have a form embedded within the child rows. However, I have encountered an issue - the input from the child rows is not being submitted if the child rows are closed. Upon further examination, I noti ...

Exploring Karma and Jasmine for Testing Angular Controllers Module

In an attempt to test my application, I have each controller defined in its own module rather than as a controller of the main app module, and then loaded as a dependency of the main app module. While running a test to check if the loginController is defin ...

Navigating the component class in Angular to access the values of an observable object

When I send an object with two values - an array and a simple variable - it is received in another component using observable. From there, I access the object values directly in the HTML template file. Below is the code snippet: Home Component: // Home ...

There seems to be an issue with the AngularJS ng-click function not functioning properly

I'm attempting to insert a link tag with a filter into an HTML content. The HTML content is bound using ng-bind-html and the link tag includes an ng-click directive. However, I'm facing an issue where the ng-click function is not functioning. He ...

What is causing the digest loop from this angular filter grouping?

My goal is to showcase a variety of items in batches of N at a time. The reason for chunking the items is because I need them to be laid out in a tabular or gridded format (each group of N items forms a row, with each item being a column). Below is my atte ...

Apply an active class to a button in an ng-repeat using ng-click

I am looking to achieve the following: Create a list that will have an active class applied when the user clicks on each item using ng-repeat Here is the code snippet I currently have: <ul class="nav nav-pills center-pills"> <li ng-repe ...

AngularJS Unveiled: The Puzzle of ng-if's Digest Loop

I am experiencing an issue with an infinite loop when loading the view. The data is retrieved from an API using ngResource in the controller. The view is being rendered multiple times before displaying correctly. I suspect that there may be a loop occurrin ...

Display all pages in the DataTables plugin while utilizing responsive tables and additional features

I am struggling to combine the responsive and fixed header attributes of my current function with the "Show All" list feature, similar to what is demonstrated in this example: https://datatables.net/examples/advanced_init/length_menu.html I need assistanc ...

Guide to retrieve the Last-Modified date using Javascript

It is common knowledge that the document.lastModified function returns a string containing the date and time when the current document was last modified. Is it possible to obtain the Last-Modified for a script? Here is an example of HTML code: ... <s ...

Monitoring changes in the AngularJS $rootScope and intercepting HTTP requests with $http for use with the ajaxSpinner directive

I am seeking help to understand why the code in ajaxSpinner directive does not recognize the changes made to $rootScope.requestCount field inside $http.defaults.transformRequest and $httpProvider.responseInterceptors. Can someone please explain this? Than ...

The useParams() function is returning undefined, even though the parameter does exist in the destination URL

I have a complete inventory of items called "Commandes" (PS: the app is in French), displayed in a table with a column for each row that should redirect me to another component showing more details about the selected row. To achieve this, I need to utilize ...