Search code examples
javascriptangularjsroutesyeoman-generator-angular

Current location with angularjs and yeoman


I have define the config of my module:

angular
  .module('myApp', [
    'ngAnimate',
    'ngCookies',
    'ngResource',
    'ngRoute',
    'ngSanitize',
    'ngTouch'
  ])
  .config(function ($routeProvider) {
    $routeProvider

      .when('/models', {
        templateUrl: 'views/models.html',
        controller: 'ModelsCtrl',
        controllerAs: 'models'
      })
      .when('/contacts', {
        templateUrl: 'views/contacts.html',
        controller: 'ContactsCtrl',
        controllerAs: 'contacts'
      })
      .when('/constructor', {
        templateUrl: 'views/constructor.html',
        controller: 'ConstructorCtrl',
        controllerAs: 'constructor'
      })
      .otherwise({
        redirectTo: '/constructor'
      });
  });

Is it possible to get information about current user state without using angular-ui-router ?


Solution

  • You can inject the $location service and make use of its path() function. Here is a link to the docs for location $location

       angular.module('app')
      .run(['$rootScope','$location', 
    function($rootScope, $location) {
        $rootScope.$on('$routeChangeSuccess', function(e, current) {
          console.log('Current route name: ' + $location.path());
        }
      }]);