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

问题描述

我刚开始学习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有此网址格式'/ visits /:name'我还要添加属性params:[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 状态中的param状态如下:

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);
}]);

希望对您有所帮助!

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

08-26 02:13