Using AngularJS location.path for unique custom URLs

Control Code:

$scope.$on('$locationChangeStart', function () {

var path = $location.path();
var adminPath = '/admin/' ;

if(path.match(adminPath)) {

  $scope.adminContainer= function() {
    return true;
  };

});

HTML

<div class="container" ng-show="adminContainer()">
 <div ui-view></div>
</div>

The code currently detects the URL matching http://localhost/angapp/#/login, but now I have additional admin URLs like below

http://localhost/angapp/#/admin/users
http://localhost/angapp/#/admin/usersGrp
http://localhost/angapp/#/admin/XYX

I want to dynamically match any URL that starts with /admin/ followed by a specific module name and display the appropriate HTML.

How can I utilize location.path() to detect Admin URLs and show/hide the div accordingly?

Answer №1

An easy solution would be to check if the URL contains the keyword "admin" and then toggle a boolean variable to control the visibility of div elements.

For example:


if ($location.absUrl().split('?')[0].match('admin')) {
    $scope.showDiv = true;
} else {    
    $scope.showDiv = false;
}

Then you can use the following code for the div:

<div ng-show="showDiv"></div>

or

<div ng-hide="showDiv"></div>

However, it is important to note that relying solely on URLs for security purposes may not be sufficient. It is advisable to explore other methods for authentication and authorization. You may find the following links helpful:

  1. https://www.sitepoint.com/implementing-authentication-angular-applications/

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

Scope variable fails to refresh in the DOM even when altered by an event listener

In my $scope object, I have a variable that is bound in the DOM. The initial value is displayed correctly when the page loads. I have set up a listener for the $window resize event, and it does get triggered appropriately when the window is resized. When t ...

Trouble with Bootstrap Popover's visibility

My current setup involves a popover that is not initializing properly. The code I am using is shown below: HTML: <div data-toggle="popover" data-placement="bottom" data-content="xyz">...</div> JavaScript: $(function () { $('[data-t ...

Incorporating video.js into an Angular website application

I've encountered a strange issue while attempting to integrate video.js into my angular app. <video id="example_video_1" class="video-js vjs-default-skin" controls preload="none" width="300" height="264" poster="http://video-js.zenco ...

Generating a constantly evolving 3D model and keeping it current within a web browser

My website already has a large user base, and I am looking to create a 3D visual representation of data on one of the pages. This model needs to be easily updatable based on each user's database information (imagine a square board with a ball whose po ...

What is the best method for directing to various pages on a GitHub Pages website?

My GitHub Pages site is live at this link. While the splash page loads perfectly, clicking on the circle to transition to the next page, 'landing.html', leads to a 404 error. I have exhausted all possible solutions to address this issue, includin ...

Angular variable resets to initial value after modification

Currently, I am utilizing ui-router to handle the state management for the admin segment of a project. The admin area includes the ability to browse through various categories. In this category management module, I am working on implementing a breadcrumb l ...

Apply a new class to all Radio buttons that were previously selected, as well as the currently

Struggling with a simple code here, trying to figure out how to add color (class) to the selected radio button and the previous ones. For example, if button No3 is selected, buttons 1, 2, and 3 should get a new color. Below is the basic code snippet: < ...

What steps are involved in creating an xlsx file using Express and Exceljs, and then sending it to the client?

I am currently working on separating controllers and services in my Express app. One of the services I have generates an XLSX file using ExcelJS. I'm looking for a way to reuse this service without passing the response object to it. Is there a method ...

Efficiently select multiple classes in Material UI with just one target

I'm currently working with Material UI and I want to update the color of my icon class when the active class is triggered by react-router-dom's NavLink Here is my code: import React from "react"; import { makeStyles } from "@mater ...

An issue with Ajax's syntax

Help needed with my ajax code. I'm encountering an error while trying to send data in Ajax - specifically with the data syntax. Despite several attempts, I have not been able to successfully resolve this issue. Here is the portion of code causing tro ...

Which Web Browsers Support Canvas and HTML5?

Currently working on a project that involves using the HTML5 Canvas element. I am curious about which major browsers (including specific versions) actually support the Canvas tag. I am not interested in hearing about IE. In this tutorial Drawing shapes - M ...

Is it possible to achieve a height transition using CSS instead of relying on JavaScript

I created these custom cards using a combination of HTML, CSS, and JavaScript. However, I've noticed that the height transition animation is not as smooth as I'd like it to be, especially on certain pages. Is there a way to achieve this animation ...

Why must the sidebar be displayed horizontally on the smartphone screen?

I've been struggling to make the sidebar menu on my smartphone display horizontally with icons at the top and text at the bottom. I've tried using flex and other methods, but it still doesn't work sideways. Am I missing something? const s ...

Expo: A guide on integrating expo code into an existing Android project

I'm looking to enhance my Android app (which is built in standard Java) by allowing users to create their own 3D models. To achieve this, I want to incorporate a 3D viewer within the app so that users can view and interact with their creations. My pl ...

Is the this.props.onChange method called within the render function?

I'm having trouble grasping the concept of the assigned onChange property in this TextField component I found in the material-ui library: <TextField style = {{"padding":"10px","width":"100%"}} type = {'number'} valu ...

Utilizing a universal JavaScript array within the jQuery document(ready) function

After using jsRender to render the following HTML template, I encountered an issue with passing data values through jQuery selectors when submitting a form via AJAX. <div class="noteActions top" style="z-index: 3;"> <span onclick="noteAction(&a ...

Having trouble with ng-model in AngularJS and Ionic's date picker functionality?

Having some trouble setting a default value in my ionic date picker, it appears to be getting ignored. The software versions being used are; Ionic "1.3.2" Node v14.15.4 AngularJS v1.8.0 ion-datetime-picker.min.js (version unknown) Here's w ...

Transferring information from AngularJS to WordPress through AJAX communication

I am looking for a way to transfer data from AngularJS to WordPress using AJAX. Within my Angular code, I have the following data: $scope.registrations.push({ id_associate: '1', activity:'demo1', qty:"1" }); $scope.registrations.push( ...

Is it possible to arrange a react map based on the length of strings?

I am working on a React function that loops through an object and creates buttons with text strings retrieved from the map. I want to display the text strings in order of their length. Below is the code snippet for the function that generates the buttons: ...

Issues with Angular route links not functioning correctly when using an Array of objects

After hard coding some routerLinks into my application and witnessing smooth functionality, I decided to explore a different approach: View: <ul class="list navbar-nav"></ul> Ts.file public links = [ { name: "Home&quo ...