45

I had two different apps in angular. During integration to a single application I had to

nest ng-views.

For sample (index.html) is

<!doctype html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="utf-8">
<title>My AngularJS App</title>
<link rel="stylesheet" href="css/app.css"/>
</head>
<body>
<ul class="menu">
<li><a href="#/view1">view1</a></li>
<li><a href="#/view2">view2</a></li>
</ul>

<div ng-view></div>

<div>Angular seed app: v<span app-version></span></div>

<script src="lib/angular/angular.js"></script>
<script src="js/app.js"></script>
<script src="js/services.js"></script>
<script src="js/controllers.js"></script>
<script src="js/filters.js"></script>
<script src="js/directives.js"></script>
</body>
</html>

One of my app view is (view2.html)

<div class="ng-view"></div>
<p>This is the partial for view 1.</p>
{{ 'Current version is v%VERSION%.' | interpolate }}

Now this application has different views once again inside it.

I tried but the page is not loading. Is there a possibility to nest ng-views?

If not Possible can it be explained?

4
  • explanation of problem is not clear at all... provide code instead of list of script tags Commented Mar 26, 2013 at 12:48
  • Currently i think nesting of ng-view is not supported if your goal is just to include external partial resource just replace nested ng-view with ng-include Commented Mar 26, 2013 at 13:19
  • I have read that there are some cross browser issues with ng-include @Ajay beni Commented Mar 26, 2013 at 13:25
  • list of alternative solutions: github.com/angular-ui/ui-router github.com/artch/angular-route-segment github.com/dotJEM/angular-routing Commented Oct 21, 2013 at 11:07

6 Answers 6

27

Updated answer:

UI Router (which now sits here: https://angular-ui.github.io/ui-router/site/#/api/ui.router) is generally regarded as the best solution for complex routing in AngularJS.


Original answer:

Nesting views isn't natively possible, as of now, in AngularJS. In my last app, I used a solution derived from here: http://www.bennadel.com/blog/2420-Mapping-AngularJS-Routes-Onto-URL-Parameters-And-Client-Side-Events.htm

Allowing me to effectively nest views (and skipping the limited ng-view altogether)

After doing so, this other (simpler, better, I believe) solution appeared:

http://angular-ui.github.com/ (scroll down to "Route Checking")

Check it out!

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

1 Comment

I started using that solution myself, but AngularUI ui-router (github.com/angular-ui/ui-router) routing is a much better solution.
24

I'd suggest that you have a look at the ui-router project by the AngularUI team. This project contains a new router based on states, which can also react to URLs, but allow way better handling of application state.

This includes the use of having multiple and / or nested views.

I had a similar question a while ago, so maybe its answers are going to help you as well: How do I setup nested views in AngularJS?

Moreover, you can expect ui-router to be integrated in AngularJS in a future version, so this will most probably be the way routing works in the future anyway. So no need to stick to other workarounds if you can already have what will be next today :-)

3 Comments

"you can expect ui-router to be integrated in AngularJS in a future version" ... AngularjS 1.2.0 will split off its routing into a separate pluggable module, but ui-router won't be merged in according to the AngularJS presentation on 1.2 and beyond (youtube.com/watch?v=W13qDdJDHp8)
Very very dissapointed upon hearing that UI-Router will not be merged.
As they move towards Angular 2.0 (blog.angularjs.org/2014/03/angular-20.html), the design team is considering how to address weakness of the native router, to include what works in the ui-router (docs.google.com/document/d/…).
2

Take a look at this:

Looks like the thing you are looking for

Comments

2

There are many third party libraries for nested views and routing. ui-router is already mentioned here, I would also suggest to take a look at this one:

http://angular-route-segment.com

It has the nested views capabilities which you ask for exactly, and it is much simpler to use than ui-router. In your example:

index.html:

<div app-view-segment="0"></div>

view1.html:

<p>This is the partial for view 1.</p>
<div app-view-segment="1"></div>

deep-view.html:

<p>This is the partial for view inside view1.</p>  

Comments

2

If you do not want to turn to yet another library to solve your problem (not that there's anything wrong with that), you should also look into using directives and ng-switch and ng-show.

This approach was given as an answer here :

angular complex nesting of partials

Comments

0

I sincerely doubt this is idiomatic Angular (and it's mentioned above that there is possible cross-browser issues), but my ng-include solution for having an "all" view with my other views nested inside something like an all.html:

    <div class="all" ng-include src="'views/foo.html'" ng-controller="FooCtrl">
    </div>

    <div class="all" ng-include src="'views/bar.html'" ng-controller="BarCtrl">
    </div>

    <div class="all" ng-include src="'views/baz.html'" ng-controller="BazCtrl">
    </div>

This worked for me but felt like it was going against the grain of the framework. I will personally be trying something like what Eamon links to on my next pass.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.