Troubleshooting: Issues with nested ui-views in Angular UI Router

I am attempting to create a basic nested route in Angular, but I am running into an issue where the view does not display when the nested route is accessed.

Even with the URL path set to

http://localhost:9000/#/home/hello
, I still only see homeHello.

Why is the nested ui view not displaying the home.hello template?

HTML Output:

<section ui-view="" class="ng-scope">
  <section class="home ng-scope">
    home
    <a ui-sref=".hello" href="#/home/hello">Hello</a>
    <!-- uiView: ui-view -->
    <div ui-view="ui-view" class="ng-scope"></div>
  </section>
</section>

Angular UI Router Configuration:

// app.js

angular.module('spoonFeeder',
  ['ui.router',
   'ngAnimate',
   'ngCookies',
   'ngResource',
   'ngSanitize',
   'ngTouch'])

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

    $urlRouterProvider.otherwise('/home')

    $stateProvider
        .state('home', {
            url: '/home',
            templateUrl: 'home/home.html'
        })
        .state('home.hello', {
            url: '/hello',
            templateUrl: 'home/hello.html'
        })

        // Uncomment the line below to use HTML5 History API
        // $locationProvider.html5Mode(true);
});

Views:

<!-- home/home`.html -->
<section class="home">home<a ui-sref=".hello">Hello</a>
  <div ui-view="ui-view"></div>
</section>

<!-- home/hello.html -->
<section class="hello">Hello</section>

Answer №1

Feel free to explore a demo on Plunker showcasing the concept of "extending the parent template".

<section class="home">home
  <a ui-sref=".hello">Hello</a>
  <div ui-view="ui-view"></div>
  <div ui-view=""></div>      <!-- new line -->
</section>

The newly added element div also includes the attribute ui-view, but in this instance, it remains unnamed for this state definition:

.state('home.hello', {
  url: '/hello',
  templateUrl: 'tpl.hello.html',
})

For demonstration purposes, introducing a new state Hello2 to demonstrate targeting the first named ui-view="ui-view":

.state('home.hello2', {
  url: '/hello2',
  views : {
    'ui-view' : {
      templateUrl: 'tpl.hello2.html',
    }
  }
})

This specific state now successfully targets the named ui-view="ui-view by utilizing an explicit views {} declaration. Conversely, the state hello utilizes an implicit views definition which can be represented as follows:

  views : {
    '' : { // targeting unnamed ui-view=""
      templateUrl: 'tpl.hello2.html',
    }
  }

Experience the changes firsthand by visiting the Plunker demo

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

Sequentially traverse the tree in reverse preorder fashion

My goal is to locate the element that is positioned directly above another element in the layout. This could mean the element is nested within several levels of the DOM structure, or it may involve moving up a few levels in the hierarchy. For instance: di ...

AngularJS - retrieving and displaying the selected value from an HTML dropdown menu

Could someone help me figure out why the Land selection is empty when trying to display it as {{ selectCenter.land }}? For reference, here is a functional plunker: http://plnkr.co/edit/Q8jhdJltlh14oBBLeHJ9?p=preview And the relevant code snippet: ...

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

Issue with Loading JQuery AutoComplete

Issue with Code Javascript Code using Jquery: $('[id$=Name]').autocomplete('CallBack.aspx',{formatItem: function(item){return item.Name;}}).result(function(event, item) { location.href = item.AGE; }); Json Data: ...

Tips on integrating multiple module dependencies in AngularJS

Can you help me figure out how to add both ['textAngular'] and ['ngDropdowns '] to my app? I attempted the following code but it's not functioning correctly: var myApp = angular.module('myApp', ['textAngular'], ...

Creating Angular classes for ng-class with functions assessment

Is there a method to dynamically assign classes to an li element based on controller function evaluations? I attempted the following approach but encountered issues: <li ng-repeat="p in projects" ng-attr-id="{{p.id}}" ng-click="selectProject(p ...

Retrieve the specific key or object number by conducting a search in JavaScript

I am faced with the challenge of editing a large XML file by searching for a specific keyword within the "event name" field and changing the corresponding "active" value to either 1 or 0. The structure of the data file is as follows. I have managed to modi ...

Nuxt - Issue persisting logged in state on refresh despite information being stored in local storage and cookies

I am facing an issue where the state gets cleared on refresh, even though the token, userid, user email, and expiration date are stored in local storage and cookies. I suspect there might be a problem with the store or something else needs to be done to re ...

Fluctuating updated site (ajax)

What method do you recommend for maintaining the data in a table on a page current? Currently, I am using a timer that connects to the server via ajax every 2 seconds to check for updates. Is there a way to trigger an event or function only when the cont ...

Steps to convert a phone number into JSON format

The primary focus Upon receiving an MQTT packet, it is displayed as an ASCII array in the buffer after being printed using stringify: packet = { "cmd": "publish", "retain": true, "qos": 1, "dup& ...

What is the best way to execute two asynchronous operations simultaneously in Node.js, treating them as a single atomic operation?

In my current setup, I have a function that calls two asynchronous functions. This main function is responsible for handling user requests. Let me show you an example: mainFunction(req, res, done) { asyncExist(req, function(exists) { if (!exists ...

Filling an HTML template with an $http response in VueJS

After learning about VueJs, I decided to embark on a small project - a nutrition app where food recommendations are made based on specific nutrients. Below is the JavaScript code snippet: recommendFood: function() { this.recs = {}; ...

The JavaScript code in Three.js is experiencing issues when running in a secure HTTPS environment

After transitioning my website from http to https, I encountered an issue with one of my projects. The background using the Three.js library in this random generator does not show up when the URL is https. However, when the URL is http, the generator wor ...

React: The getDerivedStateFromProps method does not allow the invocation of functions

I can't seem to figure out why I keep getting a TypeError - Cannot read property 'getTodosList' of null when trying to call the getTodosList function inside the getDerivedStateFromProps method. Furthermore, after implementing the getDerived ...

Navigating with Angular and Express

Currently, my Angular project is configured with Express serving my index.html file. As the project progressed, I found the need for a landing page that requires some functionality from the index.html file, such as form input that triggers an API call. H ...

Why is there an error when trying to assign Type '{}' in generics typescript?

I'm having trouble understanding why this code is causing an error. The error message says - Type '{}' is not assignable to type 'Keys<T>'. type Keys<T extends string|symbol>={ [key in T]: string; }; const foo = < ...

Guide to implementing optional localization strings in React-Router paths

Incorporating react-router, I aim to implement internationalization for links following this format: domain.com/langISO/countryISO2/page Here are some examples of valid routes: domain.com/ -> Home Page domain.com/en/us -> Home Page domain.com/fr/f ...

The significance of the "$=" or "?=" symbols in lit-element illustrations

I'm struggling to comprehend the purpose of ?= or $= in these two instances: First Example: Lit-Element README <div id="box" class$="${this.uppercase ? 'uppercase' : ''}"> <slot>Hello World</slot> </div> ...

I am utilizing client-side JS to send a large number of requests. What methods can I implement to enable my server to cache this content

Here's a bit of an unusual question from someone who is new to this - I have client-side JavaScript that is making API calls using the getJSON method. Since the content doesn't change frequently, I would like to store the results on my server an ...

Creating an Interactive Table Using HTML, JavaScript, and PHP?

After reviewing this project, I am looking to customize the js function in relation to my own table. The key focus is on the following code: $("#employee_table tr:last").after("<tr id='row"+$rowno+"'><td><input type='text&apo ...