【问题标题】:How to get the current state name using angularjs如何使用 angularjs 获取当前状态名称
【发布时间】:2016-12-02 07:44:54
【问题描述】:

我想在我的 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);

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

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

【问题讨论】:

  • 首先,你应该在这里使用 angular $http 而不是 jquery $.ajax;其次,您似乎没有将$state 注入此方法。此外,像这样使用$rootScope 是一种反模式。

标签: angularjs


【解决方案1】:

使用这个,

$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

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

【讨论】:

  • $state 是 ui-router 提供的服务,永远不需要手动设置它的任何值.....
  • @Claies 我已经更新了我的答案。你是对的。但是我手动设置,因为不清楚他需要哪个州名。
  • 如果您手动设置一个值,您可能应该使用不同的变量名称,以免将该值与该名称的服务合法提供的值混淆。
  • @Claies 你又是对的。没想到。谢谢指点。
  • 谢谢你的回答。但它仍然没有显示我当前的状态。
猜你喜欢
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 2022-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-30
  • 2023-03-17
相关资源
最近更新 更多