Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJs: Loop through / return all states in $stateProvider / ui-router

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!

like image 605
Mike Sav Avatar asked Feb 09 '23 09:02

Mike Sav


1 Answers

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"
  }
]
like image 68
Mike Sav Avatar answered Feb 12 '23 01:02

Mike Sav