如何使用angularjs获取当前状态名称

时间:2016-07-28 05:07:47

标签: angularjs

我想在run()中获取当前的州名。我正在使用$ state.current.name。但它返回空字符串。

$rootScope.$on('$stateChangeStart', function (event, next) {            
            var json = (function () {
                event.preventDefault();
                $.ajax({
                    'async': false,
                    'global': false,
                    'url':'users/testLogin',
                    //'data': 'action=showOrders', 
                    'dataType': "json",
                    'success': function (data) {
                        if(data.success){
                            $rootScope.authenticated = true;
                            $rootScope.id = data.data.id;
                            $rootScope.unique_id = data.data.unique_id;
                            $rootScope.print_house_id = data.data.print_house_id;
                            $rootScope.name = data.data.name;
                            $rootScope.email = data.data.email; 
                            $rootScope.type = data.data.type; 
                        } else {
                            console.log($state.current.name);

                        }                        
                    }
                });
            })(); 
        });

任何人都可以找出我的错误在哪里。

1 个答案:

答案 0 :(得分:2)

使用此,

$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {

         $state.current = toState;   // if you need the target Url
          $state.current = fromState;// If you need the current URL
        var json = (function () {
            event.preventDefault();
            $.ajax({
                'async': false,
                'global': false,
                'url':'users/testLogin',
                //'data': 'action=showOrders', 
                'dataType': "json",
                'success': function (data) {
                    if(data.success){
                        $rootScope.authenticated = true;
                        $rootScope.id = data.data.id;
                        $rootScope.unique_id = data.data.unique_id;
                        $rootScope.print_house_id = data.data.print_house_id;
                        $rootScope.name = data.data.name;
                        $rootScope.email = data.data.email; 
                        $rootScope.type = data.data.type; 
                    } else {
                        console.log($state.current.name);//It should show value now

                    }                        
                }
            });
        })(); 
    })