1

As I wrote in title, I can't understand how to use nested view. I'm trying to use UI-Router.

Say for example I've the following html:

<div ui-view="viewA"></div>
<div ui-view="viewB"></div>

I wish to inject two different templates for "viewA" and "viewB"

JS routes:

function configB($urlRouterProvider, $stateProvider){

    $urlRouterProvider.otherwise("/");

    $stateProvider
        .state("/",{
           url: "/",
           templateUrl : "/testingBlock.htm",
           controllerAs : "MainCTRL as ctrl"
        })
        .state("login",{
            url: "/login",
            templateUrl : "/app/templates/login.htm",
            controllerAs : "MainCTRL as ctrl"
        })
        .state("multi",{
            url: "/multi",
            templateUrl : "/app/templates/multipleView.htm",
            controllerAs : "MainCTRL as ctrl"
        });

}

HTML for "/app/templates/multipleView.htm":

<div ui-view="viewA"></div>
<div ui-view="viewB"></div>
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Donovant
  • 3,091
  • 8
  • 40
  • 68

1 Answers1

1

There is a working plunker. States defintion would be like this

// parent       
.state("multi", {
  url: "/multi",
  views: { 
    '': {
      templateUrl: "app/templates/multipleView.htm",
      controllerAs: "MainCTRL as ctrl",
    },
    'viewA@multi': {
      template: "multi parent content",
    },
    'viewB@multi': {
      templateUrl: "testingBlock.htm",
      controllerAs: "MainCTRL as ctrl",
    },
  }
})
// child
.state("multi.child", {
  url: "/child",
  views: { 
    'viewA': {
      template: "multi child content",
    },
    'viewB': {
      templateUrl: "testingBlock.htm",
      controllerAs: "MainCTRL as ctrl",
    },
  }
});

The parent contains the unnamed view '' with a template: 'app/templates/multipleView.htm', which contains our targets

<div ui-view="viewA"></div>
<div ui-view="viewB"></div>

And also, that parent state "multi" fills these targets with other views using absolute naming:

    'viewA@multi': {
      template: "multi parent content",
    },
    'viewB@multi': {
      templateUrl: "testingBlock.htm",
      controllerAs: "MainCTRL as ctrl",
    },

Child state then just injects into these two parent targets, using relative names

views: { 
    'viewA': {
      template: "multi child content",
    },
    'viewB': {
      templateUrl: "testingBlock.htm",
      controllerAs: "MainCTRL as ctrl",
    },
  }

Read more here

also this:

View Names - Relative vs. Absolute Names

Behind the scenes, every view gets assigned an absolute name that follows a scheme of viewname@statename, where viewname is the name used in the view directive and state name is the state's absolute name, e.g. contact.item. You can also choose to write your view names in the absolute syntax.

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335