8

I have a structure like this:

<div ui-view="main">
  <!-- content populated here by AngularJS ui-router -->
  <aside ui-view="sidebar">
    <!-- content populated here by AngularJS ui-router -->
  </aside>
</div>

I want to be able to define the templates in the main state like below instead of having to manage it in the child state.

.state('state1', {
  url: '/',
  views: {
    'main': {
      templateUrl: '/modules/blog/partials/index.html',
      controller: 'BlogController'
    },
    'sidebar': {
      templateUrl: '/modules/core/partials/sidebar.html'
    }
  }
});

I'd like the ui-view named sidebar to not be a child state of main and be populated by the main state's views object instead of by a child state's templateUrl field. How can I do that?

1 Answer 1

10

We can use more views inside one state, see:

The definition would just need to use the absolute naming:

.state('state1', {
  url: '/',
  views: {
    'main': {
      templateUrl: '/modules/blog/partials/index.html',
      controller: 'BlogController'
    },
    // instead of
    // 'sidebar': {
    // we need
    'sidebar@state1': {
      templateUrl: '/modules/core/partials/sidebar.html'
    }
  }
});

As explained in a detail here:

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.

So, as snippet above shows, if the content of the

 /modules/blog/partials/index.html

would be:

<div>
  <!-- content populated here by AngularJS ui-router -->
  <aside ui-view="sidebar">
    <!-- content populated here by AngularJS ui-router -->
  </aside>
</div>

and the index.html will contain placeholder:

<div ui-view="main" ></div>

Then the

  • 'main' - will be searched in parent root (index.html)
  • 'sidebar@state1' will be evaluated as viewName/target in 'state1' (itself)

An example with similar idea and some layout.html behind...

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer. It's a lot better than the ui-router docs. What about being able to do <div ui-view="main"></div><aside ui-view="sidebar"></aside>? Is that possible instead of having a nested view?
I was playing with ui-router a lot, and have to say, that this does not make sense to me. Becuase, the root <div ui-view="main"> is just a wrapping element. It is not an anchor/view target in fact. So, I would use ui-view="main" but as one of inner elements - as in my example/plunker. tpl.layout.html is injected into root (index.html), and then three areas top, left, main... are managed as needed... does it help?
If you are asking, if we should siblings ui-view="main" and ui-view="sidebar" then you've got the point! Both could contain other view targets and also could be filled via absolute names. So YES, what you say, is possible. And it is good idea! ;) Enjoy ui-router

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.