Angular UI-Router passing data between states with go function does not work

后端 未结 2 1098
慢半拍i
慢半拍i 2020-12-05 15:22

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. He

2条回答
  •  眼角桃花
    2020-12-05 15:59

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

    Hope it help you!

提交回复
热议问题