CMol
CMol

Reputation: 3

angularjs - How I can know the path of a state until another state?

I'm too new in AngularJS and I have the following question:

How I can know the path of a state until another state?

I want to know how to show all states through which it passes up to one state in particular.

e.g : I have the following States: Home, login and menuContent. If this start on menuContent and then continues to login and ends at home, I want to see the entire route or at least before the home's state that would be the last

.state('home', {
     url: "/home",
      cache: false,
        views: {
            'menuContent': {
              templateUrl: "app/pages/dashboard.html",
              controller: 'dashboardCtrl'
            },
        },
})

.state('login', {
     url: "/login",
      cache: false,
        views: {
            'menuContent': {
             templateUrl: "app/modules/login/authentication.html",
             controller: 'AuthenticationCtrl'
            },
        },
})

.state('menuContext', {
     url: "/menuContext",
      cache: false,
        views: {
            'menuContent': {
             templateUrl: "app/modules/login/menuContext.html",
             controller: 'menuContextCtrl'
            },
        },
})

I hope that point will be understood and apologize for my english

Many thanks in advance,

Regards, CMol

Upvotes: 0

Views: 65

Answers (3)

Joschka Sondhof
Joschka Sondhof

Reputation: 78

I can see two data handling options here: either create a service or stick your data to the $rootScope object.

The interesting part is how you get/store your data in the service/ object:

  • you can push data on $stateChangeStart event (like Ivan stated) and return some kind of aggregated path whenever needed
  • you can use UI Routers onEnter property in state config to push state data like so:

.

$stateProvider.state('state1.substate1', {
   url: "/substate1",
   templateUrl: "templates/substate1.html",
   controller: 'substate1Ctrl',
   onEnter: function(){
       $rootScope.path += '/substate1';
   }    
});

See docs on $stateProvider here (onEnter is listed as a function of $state).

Upvotes: 1

Joao Polo
Joao Polo

Reputation: 2163

You can create a list of objects with all rules. And then, you can register with a loop. And then, you can to access this object to generate your list.

An example code:

var rules = {
    'home': { url: "/home", cache: false, ... },
    'login': { url: "/login", cache: false, ... }
}

angular.forEach(rules, function(value, key) {
    $routeProvider.state(key, value);
});

Upvotes: 0

Ivan Iankovskyi
Ivan Iankovskyi

Reputation: 36

My suggestion would be to listen to $stateChangeStart event, in handler you will have fromState argument, so you can track those previous states and do whatever you want. You could also make some service and pass there these previous states and it will build/store full path for you.

Check out docs.

Upvotes: 0

Related Questions