2

I'm using ui-router in AngularJS like this:

.state
('browse.category', {
      url: "/:category",
      templateUrl: "views/browseCategory.html",
      controller: function($stateParams, $scope) {
        $scope.params = $stateParams;
      }
    })

How can I make the $stateParams variable global so I can access it in any view / controller?

Right now I can only access the parameters in the nested view.

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Jordash
  • 2,926
  • 8
  • 38
  • 77

2 Answers2

3

There is a working example, which uses this notation:

.run(['$rootScope', '$state', '$stateParams',
  function ($rootScope, $state, $stateParams) {
    $rootScope.$state = $state;
    $rootScope.$stateParams = $stateParams;
}])

And that makes it available anywhere:

<div >
  <h3>current state name: <var>{{$state.current.name}}</var></h3>


  <h5>params</h5>
  <pre>{{$stateParams | json}}</pre>
  <h5>state</h5>
  <pre>{{$state.current | json}}</pre>

</div>

Check it in action here

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
2

$rootScope - global variable in angular.

Alex Kvitchastyi
  • 250
  • 2
  • 11