9951 explained code solutions for 126 technologies


angularjsHow do I get the state name using AngularJS?


To get the state name using AngularJS, you can use the $location service. This service provides information about the current URL, including the current state.

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $location) {
    $scope.stateName = $location.path();
});

The $location.path() returns the current state name as a string.

Code explanation

  • var app = angular.module('myApp', []): This creates a new AngularJS module named myApp.
  • app.controller('myCtrl', function($scope, $location) {: This creates a new controller called myCtrl, which takes $scope and $location as parameters.
  • $scope.stateName = $location.path();: This assigns the state name to the stateName variable in the $scope object.
  • });: This closes the myCtrl controller.

Helpful links

Edit this code on GitHub