0

I have a post ui-view in which I would like to nest another comment ui-view within the same state (app). All views have the same controller as well.

How will I target a ui-view (comment inside the post ui-view) without using a child state. A child state would require a user to navigate to that state to view comments, I would like the posts and comments to visible as soon as a user navigates to the home page.

<!-- index.html -->
<div ui-view="post" ng-repeat="post in posts">

</div>


<!-- posts.html -->
 <div class="post">
     <p>{{post.body}}</p>
     <div ui-view="comment" ng-repeat="comment in post.comments">
     </div>
 </div>

<!-- comments.html -->
<div class="comment" >
    <p>{{comment.body}}</p>
</div>


app.config(['$stateProvider',
  function($stateProvider) {
    $stateProvider
      .state("app", {
        url: '/',
        views: {
           'posts@app': {
                    templateUrl: 'views/posts.html',
                    controller: 'PostController',
            }
        }
      })
  }
]);
2
  • 1
    Did you google "ui router nested views"? github.com/angular-ui/ui-router/wiki/… Commented Apr 23, 2016 at 18:57
  • @DavidSpence, I did. I can't find a nesting of views similar to my needs. Commented Apr 23, 2016 at 19:03

3 Answers 3

2

Per ui-router docs, you can utilize multiple views on the same page or nested views. What you have there is 2 views on the same page, in which you'll need multiple named views.

If you're wanting a view inside a view, you'll want to use the first link, where you can just use ui-view, but you have to put the second ui-view on the template of the first ui-view.

You can't put another ui-view inside the div of another ui-view. Your best option is to give them id's and then style them with css. Or if you're wanting, say, comments on a post, you'll actually want to use the second one where you create the post template, and then put a ui-view on that page for the comments.

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

Comments

0
 app.config(['$stateProvider',
  function($stateProvider) {
    $stateProvider
      .state("app", {
        url: '/',
        views: {
           'posts@app': {
                    templateUrl: 'views/posts.html',
                    controller: 'PostController',
            }
        }
.state("app.comments", {
        url: '/comments',
        views: {
           'comments@app': {
                    templateUrl: 'views/comments.html',
                    controller: 'CommentsController',
            }
        }
      })
  }
]);

1 Comment

Thank you for your answer but please check my updated question to get a better idea about what I'm shooting for.
0

From your sample you just want to reuse the comment html repeatedly within the post html. In this case you would just use ng-include to include a partial (the comment html). See ng-include in the angularjs docs. Nothing to do with ui-router.

Your ui-view maps to the post html and includes your controller. Your post html includes the comment html repeatedly.

Hope that helps

1 Comment

Thank you for your answer. I think this will work. Another idea is to use 1 state and with child states that don't change the parent states url. Then every time the home page is visited it navigates to the appropriate child state.

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.