Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS UI-Router get the current state from within a view

Considering the following states taken from the ui-router documentation:

.state('state1', {   url: '/state1',   templateUrl: 'partials/state1.html'   controller: 'State1Ctrl' }) .state('state1.list', {   url: '/list',   templateUrl: 'partials/state1.list.html', }) 

And the controller for "partials/state1.html" for state "state1":

.controller('State1Ctrl', function () {  }); 

Is there any built-in feature to determine from within the controller or within the template, what state the controller/template is associated with?

For example:

.controller('State1Ctrl', function ($state) {   console.log($state.this); // state1 }); 

And if there is no built-in solution, how would you "decorate" $state or $stateParams, to contain this information?

The only solution I've come up with is to use $state.get() and then find the state with the controller or template value. This seems incredibly messy, though.

like image 589
TaylorMac Avatar asked Jul 30 '14 15:07

TaylorMac


People also ask

What is $state in AngularJS?

$stateProvider is used to define different states of one route. You can give the state a name, different controller, different view without having to use a direct href to a route. There are different methods that use the concept of $stateprovider in AngularJS.

What is ui sref active?

[uiSrefActive] : When this selector is used, the class is added when the target state or any child of the target state is active. [uiSrefActiveEq] : When this selector is used, the class is added when the target state is exactly active (the class is not added if a child of the target state is active).

What is ui sref in AngularJS?

ui-sref stands for UI-Router state reference. It's a way to change states/state params (as defined in using the $stateProvider in a config block using the ui. router module for AngularJS. You can read the ui-sref documentation here.

What is ui routing in AngularJS?

Angular-UI-Router is an AngularJS module used to create routes for AngularJS applications. Routes are an important part of Single-Page-Applications (SPAs) as well as regular applications and Angular-UI-Router provides easy creation and usage of routes in AngularJS.


1 Answers

You can access the current state configuratin object like this:

$state.current 

For further information take a look at the $state documentation.

like image 105
Himmet Avsar Avatar answered Sep 28 '22 22:09

Himmet Avsar