2

I am trying out the nested views feature of ui-router plugin, but faced the issue I don't know how to solve. The code that shows the problem can be found at http://jsfiddle.net/3c9h7/1/ :

  var app = angular.module('myApp', ['ui.router']);

  app.config(function($stateProvider) {
    return $stateProvider.state('root', {
      template: "<div class='top' ui-view='viewA'></div><div class='middle' ui-view='viewB'></div>"
    }).state('root.step1', {
      url: '',
      views: {
        'viewA': {
          template: '<h1>View A</h1>'
        }
      }
    }).state('root.step1.step2', {
      url: '/step2',
      views: {
        'viewB': {
          template: '<h1>View B</h1>'
        }
      }
    });
  });

  app.controller('MainCtrl', [
    '$scope', '$state', function($scope, $state) {
      $state.transitionTo('root.step1.step2');
    }
  ]);

<div ng-app='myApp' ui-view ng-controller='MainCtrl'>
</div>

So, the code activates "root.step1.step2" state by using $state.go method(https://github.com/angular-ui/ui-router/wiki/Quick-Reference#stategoto--toparams--options) According to ui-router documentation:

When the application is in a particular state—when a state is "active"—all of its ancestor states are implicitly active as well.

So, I expect that "root.step1" and "root" will be active and it works as expected, but "viewB" is not filled with the template as you can see in jsfiddle sample : the top viewA(root.step1) is OK, but the middle viewB(root.step1.step2) is empty. What I am doing wrong?

sovo2014
  • 487
  • 7
  • 17

1 Answers1

4

The documentation says:

Child states will load their templates into their parent's ui-view.

So there should be a ui-view='viewB' inside the viewA template, since the parent state of root.step1.step2 is root.step1. Or the viewB should be one of the views of root.step1, and there should be no root.step1.step2.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 1
    Thanks a lot. I was able to solve the problem by using the absolute view name viewB@root in the root.step1.step2 state: http://jsfiddle.net/3c9h7/2/ But your suggestion was really helpful to figure it out. – sovo2014 May 18 '14 at 08:34