AngularJS ui router passing data between states without URL

前端 未结 2 651
死守一世寂寞
死守一世寂寞 2020-11-22 12:05

I am facing this problem of passing data between two states without exposing the data in the url, it\'s like user cannot really directly land on this state.

For exam

2条回答
  •  迷失自我
    2020-11-22 12:26

    The params object is included in $stateParams, but won't be part of the url.

    1) In the route configuration:

    $stateProvider.state('edit_user', {
        url: '/users/:user_id/edit',
        templateUrl: 'views/editUser.html',
        controller: 'editUserCtrl',
        params: {
            paramOne: { objectProperty: "defaultValueOne" },  //default value
            paramTwo: "defaultValueTwo"
        }
    });
    

    2) In the controller:

    .controller('editUserCtrl', function ($stateParams, $scope) {       
        $scope.paramOne = $stateParams.paramOne;
        $scope.paramTwo = $stateParams.paramTwo;
    });
    

    3A) Changing the State from a controller

    $state.go("edit_user", {
        user_id: 1,                
        paramOne: { objectProperty: "test_not_default1" },
        paramTwo: "from controller"
    });
    

    3B) Changing the State in html

    Example Plunker

提交回复
热议问题