Migrate from ngRoute to ui-router
Asked Answered
C

1

13

Needs some guidance with respect to migrating my ngRoute configuration to a ui.router configuration. Currently I have one main template (index.html) and it has an ng-view where all views are injected. My current ngRoute config is as follows:

app.config(function ($routeProvider) {
    $routeProvider
    .when('/login', {
        templateUrl: 'app/views/login.html',
        controller: 'LoginCtrl'
    })
    .when('/contact', {
        templateUrl: 'app/views/contact.html',
        controller: 'ContactCtrl'
    })
    .when('/notification', {
        templateUrl: 'app/views/notification.html',
        controller: 'NotificationCtrl'
    })
    .otherwise({
        redirectTo: '/login'
    });

I now want to define a second place in index.html where I can inject some view content - not a nested view, but rather another ng-view (or ui-view in ui-router terminology). The original ng-view section is the default one (currently just for /login and /contact), and the new one is just for specific routes (currently just '/notification' but maybe others in the future). Lets call the new ui-view 'notification-view'.

I've gone through much of the ui-router documentation and still am unsure of how to migrate the above to the new ui.router config. Can someone get me started or point me toward some decent examples?


Update: Ok, here is where I am. I've adding some states and a new ui-view to my index.html page. See below:

    <div class="container">     
        <div id="header"></div>
        <div data-ui-view></div>
        <div data-ui-view="notification-view"></div>
    </div>

My routing is now:

 app.config(function ($stateProvider, $urlRouterProvider) {

    $urlRouterProvider.otherwise('/login');

    $stateProvider
    .state('login', {
      url: '/login',
      templateUrl: 'app/views/login.html',
      controller: 'LoginCtrl'
    })
    .state('contact', {
      url: '/contact',
      templateUrl: 'app/views/contact.html',
      controller: 'ContactCtrl'
    })
    .state('notification', {
      url: '/notification',
      views: {
        "notification-view": {
            templateUrl: 'app/views/notification.html',
            controller: 'NotificationCtrl'              
        }
      }
    });
});

This seems to work ok for the most part. When the url /notification is triggered, the app is routed to the NotificationCtrl and renders ui-view content into the notification-view. However the only problem is that the ui content in the main (unnamed) ui-view is lost. I would like whatever is already rendered in the main ui-view to be untouched, and only target the notification-view. Is this possible? Does it have to instead be a nested-view?

Currey answered 7/11, 2014 at 18:55 Comment(3)
which template & controller you want for unnamed ui-view in "/notification" route?Rennold
@KaushickGope What do you mean unnamed ui-view? In the above routing, I have a view called 'notification-view' with: Controller=NotificationCtrl and templateUrl=app/views/notification.htmlCurrey
unnamed ui-view means what you added before 'notification-view' "<div data-ui-view></div>"Rennold
I
19

When using ui.router, you should think in terms of states rather than routes. So instead of the $routeProvider you instead inject $stateProvider, plan out various states and work from there . So from your example above, we convert it to:

app.config(function ($stateProvider,$urlRouterProvider) {

    $urlRouterProvider.otherwise('/');

    $stateProvider
    .state('login', {
        url:'/login',
        templateUrl: 'app/views/login.html',
        controller: 'LoginCtrl'
    })
    .state('contact', {
        url:'/contact',
        templateUrl: 'app/views/contact.html',
        controller: 'ContactCtrl'
    })
    .state('notification', {
        url:'/notification',
        templateUrl: 'app/views/notification.html',
        controller: 'NotificationCtrl'
    });
}

There's alot of methods for adding a "sub-view" to uirouter, one method is by adding a child state.

$stateProvider
        .state('login', {
            url:'/login',
            templateUrl: 'app/views/login.html',
            controller: 'LoginCtrl'
        })
          .state('login.error', {
            url:'/login',
            templateUrl: 'app/views/login-error-subview.html',
            controller: 'LoginErrorCtrl'
          })

Also as $stateProvider doesnt provide a default state handler, you will also need to inject in $urlRouterProvider. This is a provider that also comes with ui-router that is tasked with the responsibility of watching $location for changes.

The thing with ui-router is that you won't see a huge difference compared to the built-in route provider and ease of use it brings until you start using sub-states and stacked-states.

In your example above, ui.router wouldnt know what templte to use tor the ui-view and thus leaves it empty. You can give it a template and thus becomes:

...
.state('notification', {
      url: '/notification',
      views: {
        '':{
            templateUrl: 'app/views/notification-main.html',
            controller: ''              
        }
        'notification-view': {
            templateUrl: 'app/views/notification.html',
            controller: 'NotificationCtrl'              
        }
      }
...

But from what I'm getting you want the login and contact to have the notification in it. So ideally you'd create a notification child state for each, as right now there is now way to declare wildcard or multiple parents for a child-state. Hopefully when v1.0 comes out there'll be support for this use-case already.

Below is a link from the docs that will get you upto speed:

https://github.com/angular-ui/ui-router/wiki/URL-Routing

https://github.com/angular-ui/ui-router/wiki/Nested-States-%26-Nested-Views

Intersect answered 7/11, 2014 at 19:17 Comment(8)
Thanks for this, I will give it a try. Looking at the code though, I don't see where one can target the new ui-view (i.e. notification-view) over the default one.Currey
@Currey sorry edited should've said .state instead of .whenIntersect
So what if all my previous routing came from using $location.path()? Would the above still work? And how do I target a particular ui-view?Currey
@Currey target a ui view?Intersect
In my original post, I stated that I want some UI content to be injected into the main ui-view (the old ng-view), and other UI content to be injected into the second ui-view (which I'm calling notification-view). My understanding is that multiple views is one of the main features of ui-router. So for the state 'notification', I would like that content to only render in my new ui-view 'notification-view'. Is this possible?Currey
@Currey definitely, you have to put in a child state. edited post to include a shot snippet for a sub-view and added a link that explains further nested subviewsIntersect
Can it be done instead with non-nested views; rather using named views at the same level (inside the template index.html)?Currey
@Currey in your example above, notification is set to empty as uiview doesnt know what template to use for it. edited answerIntersect

© 2022 - 2024 — McMap. All rights reserved.