编程语言
首页 > 编程语言> > javascript – 使用go函数在状态之间传递数据的Angular UI-Router不起作用

javascript – 使用go函数在状态之间传递数据的Angular UI-Router不起作用

作者:互联网

我刚开始学习angularjs,我正在使用angular-ui-router.我试图使用$state.go将数据从一个州发送到另一个州,但我没有成功.这是我到目前为止:

我没有故意包含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被调用时,它将执行以下代码:

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

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

}]);

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

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

        console.log($stateParams);


    }]);

事物状态$stateParams是一个空对象.我希望它包含我在loginCtrl中传递的对象.

编辑

看来如果private.visits url有这个url格式’/ visits /:name’,我还添加了属性params:[“name”]我可以访问我从public.login状态发送的对象.
副作用是将参数添加到逻辑的URL.

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

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

解决方法:

你要做的是在private.visits状态中定义名称param,如:

$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 https://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;
                        }]
                    }
            }
        }
    });

然后在控制器中访问该名称:

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

希望对你有所帮助!

标签:javascript,angularjs,angular-ui
来源: https://codeday.me/bug/20190926/1822162.html