Having trouble with your UI Router states not updating correctly when changing the URL in your Angular UI Router?

I have tried numerous solutions and tutorials already with no success in finding the right answer. Therefore, I am posting here in hopes of receiving a fresh perspective and a little challenge.

Using:

angular

ui router

The Issue

I have set up different states with unique URLs. When I click on a link (ui-sref), the state and URL change correctly. However, when I manually enter a URL (e.g., "localhost:8080/user") and press 'Enter' on my keyboard, the current state switches to an 'empty state' with nothing displayed, despite the URL being correct.

Snippet of Code

Configuration:

'use strict';

angular.module('divohon-app').config(function ($stateProvider,  $urlRouterProvider, $locationProvider) {


    $locationProvider.html5Mode({
        enabled: true,
        requireBase: false
    });

    $stateProvider
        .state("user-home", {
            url: "user",
            templateUrl: "/resources/html/template/user-home.html",
            controller: "UserHome",
            controllerAs: "uhCtrl"
        })
        .state("admin-home", {
            url: "admin",
            templateUrl: "/resources/html/template/admin-home.html",
            controller: "AdminHome",
            controllerAs: "ahCtrl"
        })
        .state("login", {
            url: "login",
            templateUrl: "/resources/html/template/login.html",
            controller: "LoginController",
            controllerAs: "lCtrl"
        });
});

Any help or references would be greatly appreciated. Thank you!

Answer №1

To fix the issue, you simply need to update the url property for each state to /user, /admin, and /login respectively in your code snippet below:

$stateProvider
.state("user-home", {
    url: "/user",
    templateUrl: "/resources/html/template/user-home.html",
    controller: "UserHome",
    controllerAs: "uhCtrl"
})
.state("admin-home", {
    url: "/admin",
    templateUrl: "/resources/html/template/admin-home.html",
    controller: "AdminHome",
    controllerAs: "ahCtrl"
})
.state("login", {
    url: "/login",
    templateUrl: "/resources/html/template/login.html",
    controller: "LoginController",
    controllerAs: "lCtrl"
});

Once you make the changes above, accessing localhost:8080/user should display the correct view.

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

Merge array and object destructuring techniques

What is the correct way to remove a value from an array? const ?? = { text: ['some text'] }; ...

Ways to ensure the bootstrap table header width aligns perfectly with the body width

I am having an issue with my bootstrap table where the header width is smaller than the body width because I set the table width to auto. How can I align the header and body widths? Here is a link to a plunker showcasing the problem. https://plnkr.co/edit ...

Express server controller encountering premature return from locally executed async function

I have developed an API endpoint using Node/Express. I am trying to call a local function asynchronously within the controller function, but instead of receiving the expected asynchronous results, the called local function is returning undefined immediat ...

Updating a section of a component using another component

I need to update the Header.vue component from the ConfirmCode Component when the confirm method is called When a user logs in with axios ajax, I want to refresh the li element of the header component Appointment.vue: <send-sms-modal@clickClose="setS ...

The code is functioning properly in the output, but it does not seem to be

I have been utilizing jquery chosen to implement a specific functionality, which is successfully demonstrated in the image below within the snippet. However, upon uploading the same code to my blog on Blogger, the functionality is not working as expected. ...

Is it possible to modify the CSS styling in React using the following demonstration?

I am attempting to create an interactive feature where a ball moves to the location where the mouse is clicked. Although the X and Y coordinates are being logged successfully, the ball itself is not moving. Can anyone help me identify what I might be overl ...

MeteorJS: Verification of User Email addresses

After sending an email verification to a user, how can I ensure they actually verify their email after clicking on the link sent to their address? I'm aware of this function Accounts.onEmailVerificationLink but I'm unsure of how to implement i ...

What is the reason for the absence of the $.ajax function in the jQuery package designed for node.js?

Here is my code sample, which I would like to use for practicing with jQuery's ajax function. Note that I have already installed the jQuery package using npm install jquery: var $ = require('jquery'); var remoteValue = false; var doSometh ...

Error: Attempted to access 'embed' before it was initialized (hi)

module.exports = { name: "slowmode", description: "Set the slowmode of a channel.", execute(message, args, Discord) { if(!message.member.hasPermission("ADMINISTRATOR")) { return message.reply(&q ...

Unusual actions from the jQuery Steps extension

I am currently utilizing the jQuery Steps plugin (FIND IT HERE). The issue I'm facing lies within an IF statement that is causing the wizard to return to the first step instead of staying on the current indexed step. While all other IF statements are ...

Bizarre JavaScript Comparisons

I encountered an issue with the comparison process. The expected result should be 11 since the cost of the product at index 11 is lower than the cost of the product at index 18. However, the computed result turns out to be 18. var scores = [60, 50, 6 ...

Meteor throws a "ReferenceError: module is not defined" error message following the installation of a package

I have created a meteor package that includes the ZiggeoSDK API to seamlessly integrate Ziggeo into my meteor app. However, upon installing this package, I encountered the following error: ReferenceError: module is not defined The Ziggeo Node Server SD ...

Switching HTML text by clicking or tapping on it

I'm currently working on a website that will showcase lengthy paragraphs containing complicated internal references to other parts of the text. For instance, an excerpt from the original content may read: "...as discussed in paragraph (a) of section ...

Is it possible for a draggable position:absolute div to shrink once it reaches the edge of a position:relative div

I am facing an issue with draggable divs that have position:absolute set inside a position:relative parent div. The problem occurs when I drag the divs to the edge of the parent container, causing them to shrink in size. I need the draggable divs to mainta ...

Changing Images with Jquery on Click Event

This section of the HTML document contains an image link that is designed to switch between two images when clicked. The images in question are timeline-hand and hand-clicked. Clicking on the image should change it from one to the other and vice versa. Ho ...

Transforming the breakpoint can cause fluctuations in the jQuery UI selectable selected event

I'm running into an issue with a jQuery UI selectable div that contains child divs. The problem is that the child divs are not getting selected until I insert a breakpoint inside the selected handler. For a live example, please see my JS Fiddle: htt ...

Iterating through a JSON array using the JQuery .each method

Greetings! I'm trying to figure out how to access the msg -> items using jQuery's .each() method. { "msg": [ { "msg_id": "18628", "msg_userid": "12", "msg ...

Creating a multi-dimensional array using information from a database

I have a unique challenge where I am utilizing a template that generates a menu with a specific structure. In my database, I have various menus stored and I've successfully retrieved them. However, the issue arises when I need to figure out a way to d ...

Add fresh material to the bottom of the page using Javascript

Hey there, I'm having a bit of trouble with my page where users can post their status. I want the new posts to appear at the bottom after the older posts when the user presses the button. Currently, Ajax is placing all new posts at the top of the old ...

Is it possible for numerous identical components to trigger the display of the identical modal on a single webpage?

I am currently utilizing Vue in my project and I have a component that displays a button. When this button is clicked, it triggers a modal to open which is also part of the same component. The functionality works as intended. However, if there are multipl ...