The UI Router template fails to inject despite state changes occurring

I've been attempting to create nested views, you can check out the Plunker demo https://embed.plnkr.co/oRMnMW4QoWwhSkm9maHf/. The state is updating as expected but the template remains the same.

Could someone please point out where I may have gone wrong?

Go to Link > Second Nested .

After clicking the button, the state updates successfully but the content doesn't change. My goal is for the content of the linked page to be replaced with the content of the second nested view.

Answer №1

To properly set up abstraction, make sure to include abstract:true in the 'father' root element like this:

var routerApp = angular.module('routerApp', ['ui.router','ncy-angular-breadcrumb']);

routerApp.config(function($stateProvider, $urlRouterProvider) {

    $urlRouterProvider.otherwise('/home/list');

    $stateProvider

        // HOME STATES AND NESTED VIEWS ========================================
        .state('home', {
            url: '/home',
            abstract: true,
            templateUrl: './partial-home.html'
        })

        // nested list with custom controller
        .state('home.list', {
            url: '/list',
            templateUrl: './partial-home-list.html',
            controller: function($scope) {
                $scope.dogs = ['Bernese', 'Husky', 'Goldendoodle'];
            }
        }) 
        .state('home.second', {
            url: '/second',
            templateUrl: './second.html',
        });
});

routerApp.run(['$rootScope', '$state', function ($rootScope, $state) {
            $rootScope.$on('$stateChangeStart', function (event, toState) {
                console.log("state Change")
            });
        }]);

Keep in mind that when using abstract: true, the URL acts as a prefix and cannot be called directly in the .otherwise() function.

Including the second view in your routing, simply remove the .list like so:

.state('home.second', { //<-- REMOVE THE .list HERE
                url: '/second',
                templateUrl: './second.html',
            });

In the link, update it accordingly:

 // UPDATE IT LIKE THIS.. 

<a ui-sref="home.second" class="btn btn-danger">Second Nested</a>

Answer №2

It's quite simple - you are setting up the third level of nested states, but in your ./partial-home-list.html file, you forgot to include the ui-view directive.

To fix this, add <ui-view></ui-view> within your ./partial-home-list.html file and you will see that it functions as intended.

If you want to display the 'home.list.second' as a separate page, define the second state like this:

.state('home.second', {
            url: '/home/list/second',
            templateUrl: 'second.html',
        });

Don't forget to update the ui-sref to home.second on the button.

--

To achieve the nested breadcrumb effect, there may not be an elegant solution available, but one workaround is doing the following:


-- Partial home list html
    <div ng-if="state.current.name != 'home.list.second'">
    <ul>
    <li ng-repeat="dog in dogs">{{ dog }}</li>
    </ul>
<div ncy-breadcrumb></div>
 <a ui-sref="home.list.second" class="btn btn-danger">Second Nested</a>
</div>

<ui-view></ui-view>

App js
 // nested list with custom controller
        .state('home.list', {
            url: '/list',
            templateUrl: 'partial-home-list.html',
            controller: function($scope, $state) {
                $scope.state = $state;
                $scope.dogs = ['Bernese', 'Husky', 'Goldendoodle'];
            }
        }) 
        .state('home.list.second', {
            url: '/second',
            templateUrl: 'second.html',
        });

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

Unable to substitute 'npm run build' for 'webpack' command

I'm having trouble with npm run build not calling webpack as expected. I've modified the script in my package.json file, but it didn't work. I'm using Linux. Here is a snippet from my package.json file: { "name": "learn-webpack", ...

Replacing a function in TypeScript

Looking to alter the behavior of the alert function in TypeScript? You can achieve this by creating a new function that wraps around the original alert and modifies its behavior. alert = (function (origAlert) { return function (...messages: any[]) { ...

Leveraging JavaScript to trigger onClick() functions for multiple buttons throughout a webpage

        While searching for solutions, I came across several articles with similar topics, but unfortunately, none of the proposed solutions worked. Please forgive me if it was due to my own error. Background I am assisting a client who is transition ...

JavaScript is throwing an error related to 'typeError' even after explicitly converting to a string

Dealing with JS bugs can be quite frustrating for me. The code snippet below is giving me trouble, as it creates a string containing session data and date information to push into an array: var _writes = String(req.session.subscriber + ":" + req.session.po ...

Fixed Position - Make width match the container's dimensions

I've been experimenting with setting a div's position to fixed once a user has scrolled a certain distance. I've run into an issue where the fixed position div's width doesn't match its parent element. How can I ensure that the fix ...

Utilize the data-toggle attribute to retrieve a unique variable name, which can then be integrated into a function for added

I'm in the process of replacing an iframe with an image, and I want to accomplish this with minimal code. The container div I'm working with has a data-toggle attribute: <div id="my-div" data-toggle="video2"> <div id="videocontaine ...

Developing Modules in NodeJS using Constructors or Object Literals

I am currently developing a nodejs application that needs to communicate with various network resources, such as cache services and databases. To achieve this functionality, I have created a module imported through the require statement, which allows the a ...

Encountered an issue while attempting to create a Higher Order Component using React and

Encountered an issue while using recompose to create a Higher Order Component (HoC) with withState and lifecycle: warning.js?8a56:36 Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM eleme ...

Utilizing the variable $this outside of an object context within an Ajax function

Encountering the following error: Fatal error: Uncaught Error: Using $this when not in object context in E:\xampp\htdocs\StudentGuideBook\Version1.0\models\AjaxChecking.php:4 Stack trace: #0 {main} thrown in E:\xampp&b ...

How to effectively clear the browser cache in AngularJS

Whenever I make modifications to my Angular template (html file), I find myself having to clear my browser cache and refresh the browser in order for the changes to take effect. Is there a workaround for this inconvenience? I primarily use Chrome and Fire ...

Error: Query is not valid

I'm encountering an error when trying to run this query, and I'm not sure why it's returning a bad request. My goal is to fetch a specific user from the database using the accountId. Can someone assist me with this issue? Below is the funct ...

Determining When the Collapse Transition in Material UI 5 is Complete

Snippet <Collapse in={expanded} onTransitionEnd={() => console.log('finished')} > <div>foo</div> </Collapse> Error Detection The callback function (onTransitionEnd) is not triggered af ...

Is it possible to run two commands in npm scripts when the first command initiates a server?

When running npm scripts, I encountered an issue where the first command successfully starts a node server but prevents the execution of the second command. How can I ensure that both commands are executed successfully? package.json "scripts": { "dev ...

Guide on displaying a partial template upon clicking a specific menu item using Meteor JS

I am a newcomer to Meteor JS and I am looking for a way to render a specific template to a specific part of my web app when a menu is clicked. I am currently using iron-router and layout template. The current layout is functioning correctly. For example, w ...

Guide to reference points, current one is constantly nonexistent

As I work on hosting multiple dynamic pages, each with its own function to call at a specific time, I encounter an issue where the current ref is always null. This poses a challenge when trying to access the reference for each page. export default class Qu ...

Vue.js - The error message "$slots have 'el' is null" indicates a problem with the element in

When trying to access the Vuejs $slots instance, I encounter el = null, but type = "div" Here is the template: <slot name="head"> <h1> {{ text }} </h1> </slot> And in the script section: ... ...

Using JavaScript to implement form authorization in ASP.NET Web API

I am looking to revamp a business application by utilizing asp.net web api as the service layer and implementing JavaScript to interact with the web api for retrieving and displaying data. While I have a good grasp on how all the scenarios will function s ...

Having trouble displaying a list in HTML using ngFor syntax

I'm relatively new to angular and front-end development. I'm currently trying to make a call to a REST API in order to fetch a list of products, and then display them on the HTML page. product.component.html <div class="page-title"& ...

Is it possible to utilize a single controller for multiple views?

I have set up a single controller and utilized it in two views with minor variations. Here is the Angular code snippet: app.controller('MyCtrl', function($scope) { $scope.canSave = false; $scope.demo = { files : [{ filename ...

Parameterized translation in Angular

I have been working on implementing localization in my angular application using angular-translate. However, I have encountered a challenge when it comes to parameterized translation in a plural context. My index.html file includes the following script re ...