本文介绍了Angular UI-Router 在具有 go 功能的状态之间传递数据不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习 angularjs,我正在使用 angular-ui-router.我正在尝试使用 $state.go 将数据从一种状态发送到另一种状态,但没有成功.这是我目前所拥有的:

I just started learning angularjs and I am using angular-ui-router. I am trying to send data from one state to another using $state.go but I have no success. Here is what I have so far:

我没有故意包含 html,因为我认为不需要它,如果需要请告诉我,我会添加它.

我已将状态配置如下:

  $stateProvider
        .state('public', {
            abstract: true,
            templateUrl: 'App/scripts/main/views/PublicContentParent.html'
        })
        .state('public.login', {
            url: '/login',
            templateUrl: 'App/scripts/login/views/login.html',
            controller: 'loginCtrl'
        })

    $stateProvider
        .state('private', {
            abstract: true,
            templateUrl: 'App/scripts/main/views/PrivateContentParent.html'
        })
        .state('private.visits', {
            url: '/visits',
            views: {
                'main': {
                    controller: 'visitsListCtrl',
                    templateUrl: 'App/scripts/visits/views/VisitsList.html'
                }
            }
        });

当我的 LoginController 被调用时,它将执行以下代码:

When my LoginController is invoked it will execute the below code:

loginModule.controller('loginCtrl', ['$state', function ($scope, $state) {

    $state.go('private.visits', { name : "Object"});

}]);

private.visits 页面处于活动状态时,我试图打印 $stateParams:

When the private.visits page is active, I am trying to print the $stateParams:

visitsModule.controller('visitsListCtrl', ['$stateParams',
    function ($stateParams) {

        console.log($stateParams);


    }]);

正如事情所说,$stateParams 是一个空对象.我希望它包含我在 loginCtrl 中传递的对象.

As things state $stateParams is an empty object. I expected it to to contain the object I passed in loginCtrl.

编辑

看来,如果 private.visits url 具有这种 url 格式 '/visits/:name' 并且我还添加了属性参数: ["name"] 我访问我从 public.login 状态发送的对象.副作用是参数被加到url中,这是逻辑.

It seems that if private.visits url has this url format '/visits/:name' and I also add the property params: ["name"] I get access to the object I send from the public.login state.The side effect is that the parameters are added to the url which is logical.

我尝试对没有 url 的子状态做同样的事情,在这种情况下,我似乎无法访问从 public.login 传递的参数.

I tried doing the same thing with a child state with no url, and in this case it seems that I do not have access to the params I passed from public.login.

如何在子状态下发送数据?

How do I send data in child states?

推荐答案

您需要做的是在 private.visits 状态中定义 name 参数,例如:

What you have to do is to define the name param in the private.visits state like:

$stateProvider
    .state('public', {
        abstract: true,
        templateUrl: 'App/scripts/main/views/PublicContentParent.html'
    })
    .state('public.login', {
        url: '/login',
        templateUrl: 'App/scripts/login/views/login.html',
        controller: 'loginCtrl'
    })

$stateProvider
    .state('private', {
        abstract: true,
        templateUrl: 'App/scripts/main/views/PrivateContentParent.html'
    })
    .state('private.visits', {

        // NOTE!
        // Previously, we were only passing params as an array
        // now we are sending it as an object. If you
        // send this as an array it will throw an error
        // stating id.match is not a function, so we updated
        // this code. For further explanation please visit this
        // url http://stackoverflow.com/a/26204095/1132354
        params: {'name': null},

        url: '/visits',
        views: {
            'main': {
                controller: 'visitsListCtrl',
                templateUrl: 'App/scripts/visits/views/VisitsList.html',
                resolve: {
                        name: ['$stateParams', function($stateParams) {
                            return $stateParams.name;
                        }]
                    }
            }
        }
    });

然后在控制器中获取名称:

And then in the controller access to the name:

visitsModule.controller('visitsListCtrl', ['name',
function (name) {
    console.log(name);
}]);

希望对你有帮助!

这篇关于Angular UI-Router 在具有 go 功能的状态之间传递数据不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 02:13