1

I have states mentioned as

        .state('login', {
            url: '/login',
            templateUrl: 'views/login.html',
            controller: '',
            ncyBreadcrumb: {label: 'Home'},
            data: {freeRoute: true}
        })
        .state('dashboard', {
            url: '/',
            templateUrl: 'views/login.html',
            controller: '',
            ncyBreadcrumb: {label: 'Dashboard'},
            data: {authenticatedRoute: true},
        })

I wish to run configuration in case if page is refreshed, like this

    .run(function ($rootScope, $state, $stateParams, AUTH_EVENTS, dataService) {
    if ($state.$current.data.authenticatedRoute == true) {
        console.log('Authenticated Route.');
        if (dataService.isAuthenticated()) {
            $rootScope.$broadcast(AUTH_EVENTS.alreadyAuthenticated);
        } else {
            $rootScope.$broadcast(AUTH_EVENTS.notAuthenticated);
        }
    } else {
        console.log('Not Authenticated Route.');
    }
    });

But when I tried to log console on current state it gives me like this...

{"name":"","url":"^","views":null,"abstract":true}

-- How would I get current state. in controller? I have also tried console.log($state.$current); -- How would I access in views as well, irrespective of controller?

Sankalp
  • 1,300
  • 5
  • 28
  • 52
  • 1
    The way I'd use is not just a .run() but listener on the `$rootScope.$on('$stateChangeStart'`... check it e.g. here http://stackoverflow.com/a/26702638/1679310 – Radim Köhler Oct 25 '15 at 06:30
  • It has been done using $stateChangeStart. THanks – Sankalp Oct 26 '15 at 11:33
  • Really? ;) great to see that. Enjoy mighty UI-Router, sir – Radim Köhler Oct 26 '15 at 11:34
  • I have question related to this. Can I modify this or ask another question? I have issue of 'too much recursion'. Because I change state in broadcast method. – Sankalp Oct 26 '15 at 11:43
  • You should issue a question, because here you have just me, with new question your audience and chance to get right answer will be increased... – Radim Köhler Oct 26 '15 at 11:44
  • ;) My playground is SO only. And I do answer only if I know *(I cannot guarantee that for each scenario, right ;)*. So, I'd really suggest: ask a new question here, and someone smart will hopefully give you the answer. – Radim Köhler Oct 26 '15 at 11:51
  • I requested u on SKP. Would be pleasure to add you. – Sankalp Oct 26 '15 at 11:52

1 Answers1

0

You should be able to use console.log($state.current) to be able to see currently active state's name, parent, url etc.

BTW, if you want to quickly see this detail in debug console using command line, you could use following kind of command in dev tool's debug console:

$("html").injector().get("$state").current
rbhatt
  • 1
  • 1