4

this is a silly question, but should I wish to return all instances of $stateProvider.state in my app.js (my AngularJS config file) how would I go about this? For example when I used the ngRoute I could do something like this in my .run() block:

.run(function ($route) {

     var someArray = [];
     angular.forEach($route.routes, function (route, path) {
          if (route.someKey) {
            console.log(path); // do something with route.someKey!!!
            someArray.push(route.someKey);
          }
   });
});

However, in my app I am using the ui-router and I can't do something similar with $state? I'm currently reading up on this but if anyone has a quick answer let me know!

3
  • What would you want to do in this "do something" ? Are you just trying to retrieve the current state ? Commented Aug 5, 2015 at 13:04
  • I would be checking the value of route.somekey and then performing an action like making an array and then using this later to determine access Commented Aug 5, 2015 at 13:05
  • Oh, like having the history of the user navigation ? I love the idea. You could do this by using the $scope.$on("$stateChangeSucess") Commented Aug 5, 2015 at 13:20

1 Answer 1

8

Found it! $state.get() works! I must have missed this before in the docs!

$state.get() will output an object array, I run console.log($state.get); and I get:

[
  {
    "name": "",
    "url": "^",
    "views": null,
    "abstract": true
  },
  {
    "abstract": true,
    "template": "<ui-view/>",
    "name": "app"
  },
  {
    "url": "/login",
    "templateUrl": "login/views/login.html",
    "controller": "LoginCtrl",
    "publicAccess": true,
    "name": "app.login"
  },
  {
    "abstract": true,
    "url": "/home",
    "templateUrl": "home/views/home-wrapper.html",
    "controller": "HomeCtrl",
    "name": "app.loggedInHome"
  }
]
Sign up to request clarification or add additional context in comments.

Comments

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.