1

I have a parent route which loads a list of objects in the resolve function. After the user selects a item of this list it loads the child route. The child route appends the itemId to the url. Now I would like that the parent automatically "redirects" to the first item of the list and therefore change to the child route.

I tried calling $state.go in the resolve function after the promise was resolved but that started a endless redirect cycle.

Here is my current setup.

    $stateProvider.state("parent", {
      url: "/parent/:parentId/children",
      templateUrl: "parentTempl.html",
      controller: "ParentController",
      controllerAs: "vm",
      resolve: {          
        ParentLoadingService: function ($stateParams, ResourceService) {
          return ResourceService.request.getChildren({ parentId: $stateParams.parentId }).$promise;
        }
      }
    }).state("parent.child", {
      url: "/:childId",
      templateUrl: "child.html",
      controller: "ChildController",
      controllerAs: "vm",
      resolve: {
        ChildLoadingService: function ($stateParams, ParentLoadingService) {
          ...
        }
      }
    });

thanks

KenavR
  • 3,769
  • 9
  • 37
  • 47
  • 1
    Would not this solution http://stackoverflow.com/q/29491079/1679310 work for you? – Radim Köhler May 21 '15 at 09:48
  • This may work, I will take a look at it. But I hoped I could solve this in the route specification. Adding logic to the controller, $rootScope or run() seems not really desirable. – KenavR May 21 '15 at 09:58
  • The native solution is desribed here http://stackoverflow.com/q/23658846/1679310 but because there was an issue until 0.2.14 we have to find solution http://stackoverflow.com/q/27120308/1679310 and even better is the link I've sent above. With 0.2.15 it should be fixed so the .when() could work as well. – Radim Köhler May 21 '15 at 10:07
  • I used your first solution works great. I guess one more or less function on $rootScope won't hurt. thanks – KenavR May 21 '15 at 11:50
  • ;) ;) Great to see that ;) Enjoy mighty UI-Router sir! – Radim Köhler May 21 '15 at 11:51

0 Answers0