Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular routing ui-view inside another ui-view

Conception overview:

We have two tabs on index html. There we routing those tabs like that:

<div ui-view></div>

On a second tab we have a selector, that switch tab's content in another ui-view like that:

<div ui-view="{{vm.currentView}}"></div>

where vm.currentView is a name of routing state ('book1' and etc.).

 .state('tab2', {
        abstract: true,
        templateUrl: 'tab2.html',
        controller: 'Tab2Controller',
        controllerAs: 'vm'
    })
    .state('tab2.content', {
        url: '/tab2',
        views: {
            '': {
                templateUrl: 'tab2.html'
            },
            'book1@tab2': {
                templateUrl: 'tab2-book1.html'
            },
            'book2@tab2': {
                templateUrl: 'tab2-book2.html'
            },
            'book3@tab2': {
                templateUrl: 'tab2-book3.html'
            },
            'book4@tab2': {
                templateUrl: 'tab2-book4.html'
            }
        }
    });

Everything is fine, except one thing: data content and name of a view is changing, but a template content isn't.

I resolved it by another way (based on exclude 'ui-view inside another ui-view' conception and separate views in states). But i still want to know: "How to do this with using 'ui-view inside ui-view' conception?"

Here's a Plunker Example

like image 615
SanSan- Avatar asked Nov 13 '15 15:11

SanSan-


1 Answers

Its possible to make 'ui-view inside another ui-view'.

Lets say you have an index.html

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

and state provider is like this :-

$stateProvider
   .state('books', {
        parent: 'pages',
        url: '/books',                   
        views: {
            'content@': {
                 templateUrl: 'books.html',
                 controller: 'BooksController'
             }
        }
   })

In books.html you have some links and another ui-view (nested ui-view). On click of links populate the nested ui-view.

books.html

<div>
     <a ui-sref="book1"></a>
     <a ui-sref="book2"></a>
     <a ui-sref="book3"></a>
</div>
<!-- nested ui-view -->
<div ui-view="bookDetails"></div>

now state provider is :-

 $stateProvider
    .state('books', {
        parent: 'pages',
        url: '/books',                   
        views: {
            'content@': {
                 templateUrl: 'books.html',
                 controller: 'BooksController'
             }
        }
    })
    .state('book1', {
        parent: 'books',                  
        views: {
            'bookDetails@books': {
                 templateUrl: 'book1.html',
                 controller: 'BookOneController'
             }
        }
    })
    .state('book2', {
        parent: 'books',                 
        views: {
            'bookDetails@books': {
                 templateUrl: 'book2.html',
                 controller: 'BookTwoController'
             }
        }
    })
    .state('book3', {
        parent: 'books',                
        views: {
            'bookDetails@books': {
                 templateUrl: 'book3.html',
                 controller: 'BookThreeController'
             }
        }
    })

bookDetails@books :- populate 'bookDetails' ui-view in 'books' state or we can say that find 'bookDetails' ui-view inside 'books' state and populate it with 'views' object.

like image 187
Sanjay Singh Rawat Avatar answered Nov 08 '22 00:11

Sanjay Singh Rawat