Toggle the availability of links based on the presence of route parameters

My routing configuration appears as follows (for readability, I've omitted the second parameter in when()):

app.config(function($routeProvider, $locationProvider){

    // Prepare for html5 mode
    $locationProvider.hashPrefix('!');
    $locationProvider.html5Mode(true);

    // Setup routing
    $routeProvider.when('/dashboard/main', {
    }).when('/dashboard/website/:id/clicks', {
    }).when('/dashboard/website/:id/uploads', {
    }).when('/dashboard/website/:id/forms', {
    }).when('/dashboard/website/:id', {
    }).otherwise({
        redirectTo: '/dashboard/main'
    });

});

Subsequently, I have navigation links that I wish to disable when :id is absent.

<ul class="nav nav-pills nav-stacked">
    <!-- always active -->
    <li>
        <a href="/dashboard/main"><i class="glyphicon glyphicon-home"></i> Home</a>
    </li>
    <!-- active only if :id is set -->
    <li>
        <a href="/dashboard/clicks"><i class="glyphicon glyphicon-hand-up"></i> Clicks</a>
    </li>
    ...
</ul>

Is there a way to automatically enable the second navigation item when :id is included, and disable it when :id is not present?

Answer №1

If you want to utilize $routeParams within the controller, you can verify it by:

$scope.hasOptions = $routeParams.id !== 'undefined'

Then, modify the view to display:

<li ng-show='hasOptions'>
        <a href="/dashboard/clicks"><i class="glyphicon glyphicon-hand-up"></i> Clicks</a>
    </li>

Answer №2

To utilize $routeParams in your controller, follow the code below:

app.controller('yourControllerName', ['$routeParams', '$scope', function($routeParams, $scope){
$scope.isActive = inactive;
if($routeParams.id){
    $scope.isActive = active;
}
}]);

In your HTML markup:

<li data-ng-class="isActive">
        <a href="/dashboard/clicks"><i class="glyphicon glyphicon-hand-up"></i> Clicks</a>
</li>

Afterwards, define appropriate styles for the .inactive and .active classes using CSS

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

How can I create walls in ThreeJS using a path or 2D array?

I am faced with the task of creating a 3D house model, specifically the walls, using a 2D path or array that I receive from a FabricJS editor that I have developed. The specific type of data being transferred from the 2D to 3D views is not critical. My in ...

What is the proper method for delivering Javascript code with rendered HTTP to a client?

During the development process, I made a decision to switch to server-side rendering in order to have better control and take advantage of other benefits. My web application relies heavily on AJAX with no url redirecting, creating a website that dynamicall ...

achieve precise outcomes using mapping techniques

I am currently learning react.js and encountering an issue with obtaining specific results on the map. Below is the code I am working with: render(){ const friends = [ {id:1, name: 'Dave',age:50}, {id:2,name: 'Kellie',age:42}, {id:3, ...

Are two identical directives conflicting with each other?

In order to identify the bug I am experiencing, I decided to create two instances of the same directive in my view : <criterion-news state="ctrl.state"> </criterion-news> <criterion-news state="null"> </criterion-news> The fir ...

What is the process for node_modules packages to access configuration files located in the project root directory?

I am currently developing an npm package that requires the ability to access configuration files from the project's root directory. I'm uncertain of the proper method for accomplishing this. For instance, Next.js has the capability to read ./p ...

Clicking the checkbox does not synchronize with the table row

Is there a way to activate the button when the user selects the checkbox directly? Currently, the button only becomes enabled when the user clicks on a table row that is clickable. var btn = $("#btn"); btn.attr("disabled","disabled"); $("#table tbo ...

Sending data from a child component to a parent component in React

Trying to figure out how to pass the returned value of function appColor from the WeatherForecast component into a property, and then passing that property from WeatherForecast to the className of the app component. I'm new to React, so not sure how t ...

React Quill editor fails to render properly in React project

In the process of developing an application in Ionic React, I am in need of a rich text editor that can save data on Firestore. Despite initializing the editor as shown below and adding it to the homepage component, it is not rendering correctly although i ...

Trigger a click event in jQuery to activate a form through a hyperlink

I'm facing an issue where I want to implement a password-reset form based on a certain flag being triggered. Currently, my code is set up to prompt the user to change their password if the password_changed_flag is not present. Depending on the user&ap ...

Implementing swipe functionality to Bootstrap carousel

This is the code snippet I am working with: <div class="container"> <div id="myCarousel" class="carousel slide" data-ride="carousel" data-interval="3000"> <!--Indicators--> <ol class="carousel-indicators"> ...

Updating an existing value with a cascading dropdown list

My JavaScript code dynamically populates a dropdown list called District based on the selection made by the user in another dropdown list called Department. Here is the snippet of the code: Firstly, I populate the Department dropdownlist and add a ' ...

By pairing delay(0) with refCount(), we can achieve optimal efficiency

The refCount operator is discussed in this article. It explains the necessity of adding delay(0) to prevent unsubscription of observable A: import { Observable } from "rxjs/Observable"; const source = Observable.defer(() => Observable. ...

Transform the JSON structure with the power of JavaScript

Seeking assistance in converting the following array of JSON using either javascript or jquery: [ [{"day1":10,"day2":154,"day3":24,"day4":48,"day5":154,"day6":48,"day7":154,"name":"Packet"}], [{"day1":10,"day2":154,"day3":24,"day4":48,"day5":154,"day6":4 ...

An error is thrown when using less.js

Every time I attempt to add less.js, I encounter an XmlHttpRequest Exception 101. Including the .less file is done using this method: <link rel="stylesheet/less" type="text/css" href="anything.less" /> This issue only arises when I upload the th ...

The parseFloat function only considers numbers before the decimal point and disregards

I need my function to properly format a number or string into a decimal number with X amount of digits after the decimal point. The issue I'm facing is that when I pass 3.0004 to my function, it returns 3. After reviewing the documentation, I realized ...

What is the best way to search for items using only a portion of a phone number or typing a name in any case (uppercase, lowercase) and still get accurate results?

let contacts = [ { name: 'John', phone: 987654 }, { name: 'Sara', phone: 654321 } ] I am developing a contact manager app with various functions to manage contacts. Add new contac ...

Accessing new information seamlessly without the need for page refresh

For optimal mobile viewing of my website, I am considering implementing a select element. Here are the available options: HTML: <select name="select-choice-8" id="select-choice-nc"> <optgroup label="News"> & ...

How can I display two slides at once in Ionic 2/3 on a wide screen?

I have been searching for a solution that will allow me to display 1 ion-slide if the device screen is small, but if it's larger, then I want to display 2 ion-slides. Unfortunately, I have not been able to find a suitable solution yet. As an example, ...

Use leaflet.js in next js to conceal the remainder of the map surrounding the country

I'm currently facing an issue and would appreciate some assistance. My objective is to display only the map of Cameroon while hiding the other maps. I am utilizing Leaflet in conjunction with Next.js to showcase the map. I came across a helpful page R ...

Edge Browser does not support PHP Websocket technology

I created a multiplayer card game and incorporated a websocket for functionality. To integrate the websocket in php, I utilized this specific library After deploying it on my Ubuntu server, the program functioned smoothly on Chrome and Firefox (The fronte ...