Angular routes are failing to update the view even after attempting to refresh

I am facing an issue while trying to develop an angular app where the child state is not loading properly.

app.config(function ($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise("/");

    $stateProvider
        .state('home', {
            url        : "/",
            templateUrl: "/admin/home/"
        })
        .state('users', {
            url        : "/users/",
            templateUrl: "/admin/users",
            controller : function ($stateParams) {
                console.log($stateParams);

                console.log("users");
            }
        })
       .state('users.new', {
           url        : "^/users/user/",
           templateUrl: "/admin/users/new",
           controller : function () {
               console.log("users.new");
           }
        })
        .state('users.user', {
            url        : "^/users/user/:uuid",
            templateUrl: function ($stateParams) {

                console.log($stateParams);

                return "/admin/users/" + $stateParams.uuid
            },
          controller : function () {
              console.log("users.user");
          }
        });

When I try to access the URL

/users/user/55d8b1c706387b11480d60c1

I can see that the request to load the page is made, but only the "users" controller is executed.

This issue seems to occur specifically with child states, as switching between parent states is functioning correctly.

I am using the latest versions of Angular and UI-Router.

Any suggestions on how to resolve this?

Answer №1

Make sure to review this documentation

The error lies in the hierarchy structure. Child states rely on the parent template as the root and search for nested <ui-view> elements.

To address this, consider adding an abstract state named user without a URL and with an empty template like <ui-view></ui-view> for nested views. Then, rename it to something like user.index. This approach worked for me.

One way to implement this is:

$stateProvider
    .state('home', {
        url        : "/",
        templateUrl: "/admin/home/"
    })
    .state('users', {
        template: "<ui-view></ui-view>",
    })
    .state('users.index', {
        url        : "/users/",
        templateUrl: "/admin/users",
        controller : function ($stateParams) {
            console.log($stateParams);

        console.log("users");
    }
})
.state('users.new', {
    url        : "^/users/user/",
    templateUrl: "/admin/users/new",
    controller : function () {
        console.log("users.new");
    }
})
.state('users.user', {
    url        : "^/users/user/:uuid",
    templateUrl: function ($stateParams) {

        console.log($stateParams);

        return "/admin/users/" + $stateParams.uuid
    },
    controller : function () {
        console.log("users.user");
    }
});

Another approach is to use absolute named views. For example:

views: { 
      "": { templateUrl: 'pages/menu.html' }, // relative noName view
      "header": { templateUrl: "pages/header.html"}, //relative named view
      "content@": { // Absolute name
           template: "<ui-view></ui-view>",
           controller: 'AuthController'}
    }

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

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 ...

How can we implement :focus-within styling on Material-UI Select when the input is clicked?

I am currently implementing a Select component inside a div element: <div className="custom-filter custom-filter-data"> <DateRangeIcon className="search-icon"/> <FormControl variant='standard& ...

Exploring Angular 2 Tabs: Navigating Through Child Components

Recently, I've been experimenting with trying to access the HTML elements within tabs components using an example from the Angular 2 docs. You can view the example here. Here is a snippet of my implementation: import {Component, ElementRef, Inj ...

Retrieve the CSS selector for the element that my plugin has been implemented on

Currently, I am in the process of developing a jQuery Plugin that is designed to be applied on a specific container element like so. $('#container').myPlugin(); Within this plugin, my goal is to retrieve the same element from another page using ...

AngularJS $routeProvider is experiencing difficulties with its routing functionality

I am a beginner with Angular and I'm trying to understand how multiple routes can lead to the same view/templateUrl and controller. This is what I have written: angular .module('mwsApp', [ 'ngAnimate', 'ngCookies&ap ...

Generating variables dynamically within a React Native component

In my React Native component, I need to create a variable that will be used multiple times. Each instance of this component should have a different variable name for reference. <View ref={view => { shapeView = view; }} onLayout={({ nativeE ...

The result from the AngularJs promise is coming back as undefined

I am facing an issue while trying to implement the login function of my AuthService factory in conjunction with my AuthLoginController controller. The problem arises when the User.login function is triggered with incorrect email and password details, causi ...

AJAX call error: Invocation not permitted

I'm currently working on a web application using JQuery that requires communication with a server. I've reused this code multiple times, only changing the data and success function. However, every time I execute this function, I encounter an err ...

Is it possible for me to ensure that the argument passed to `res.write` is sent as one solid chunk?

When utilizing express' res.write method to send chunks back to the requestor, it is important to note that one call to res.write does not necessarily mean the argument passed will be received as a single chunk. This can complicate the process of pars ...

What is the method to access 'let' variables in the console of a developer tool?

When you open the Chrome devtool and enter the code snippet below: // The iife is irrelevant let r = (() => { return 2; })(); and then evaluate r, the output will be: r 2 Surprisingly, both window.r and globalThis.r return undefined. Although let is ...

Using jQuery and AJAX to fetch the return value of a PHP class method

Starting a new project for myself has been quite the learning experience. As someone who isn't the most skilled programmer, I decided to kick things off in PHP. However, I quickly realized that executing PHP functions and class methods through HTML bu ...

Manipulating DropDownList Attributes in ASP.NET using JavaScript

I am facing an issue with populating a Dropdownlist control on my ASCX page. <asp:DropDownList ID="demoddl" runat="server" onchange="apply(this.options[this.selectedIndex].value,event)" onclick="borderColorChange(this.id, 'Click')" onblur="bo ...

Tree Grid in jqGrid with Offline Data

Accessing the jqGrid documentation for tree grid, I discovered the following information: "At present, jqGrid can only interact with data that is returned from a server. However, there are some tips and resources available that explain how to work w ...

React Full Calendar Error: Unable to access property 'calendar' from undefined

I'm encountering an issue while attempting to save selected time information in state. Any assistance would be greatly appreciated, thank you for your help! Please feel free to ask if more specific details are required. Below is a snippet of code fro ...

Automatically updating client-side values in AngularJS using setInterval()

Implementing a value calculator on the client side using AngularJS. I need to update the main value of the calculator every 5 minutes with setInterval(). This is my AngularJS code: $http({method: 'GET', url: '../assets/sources.json'} ...

Darken the entire webpage and gradually fade in a specific div element using Jquery

Currently, I am working on implementing the following functionality: - When a link is clicked, it should trigger a function to show a DIV (#page-cover) that will dim down the entire background. This div has a z-index of 999. - Following the dimming effect, ...

Issue: Catching errors in proxy function calls

I am currently using Vue 3 along with the latest Quasar Framework. To simplify my API calls, I created an Api class as a wrapper for Axios with various methods such as get, post, etc. Now, I need to intercept these method calls. In order to achieve this ...

Prevent the bottom row from being sorted

I have implemented sortable table rows in my angular project, however the sorting functionality also affects some query-ui code elements. Now I am looking to exclude the last row from being sortable. HTML <div ng:controller="controller"> <ta ...

Tips for incorporating a plugin and utilizing an external module or file on RT

My node.js application/module is currently functioning well with a plug-in concept. This means that my module acts like a proxy with additional capabilities, such as adding new functionality to the existing methods. To achieve this, follow these steps: Cl ...

Tips for returning JSON data using AJAX

When working with native JS, I am familiar with using AJAX to display the output from PHP/mySql that is not Json Encoded in the element "some_id" like this: <script> function addItem(value) { xmlhttp = new XMLHttpRequest(); xmlhttp.onrea ...