`ui-router` $stateParams vs. $state.params

前端 未结 7 970
隐瞒了意图╮
隐瞒了意图╮ 2020-12-07 08:07

With ui-router, it\'s possible to inject either $state or $stateParams into a controller to get access to parameters in the URL. Howev

7条回答
  •  半阙折子戏
    2020-12-07 08:31

    I have a root state which resolves sth. Passing $state as a resolve parameter won't guarantee the availability for $state.params. But using $stateParams will.

    var rootState = {
        name: 'root',
        url: '/:stubCompanyId',
        abstract: true,
        ...
    };
    
    // case 1:
    rootState.resolve = {
        authInit: ['AuthenticationService', '$state', function (AuthenticationService, $state) {
            console.log('rootState.resolve', $state.params);
            return AuthenticationService.init($state.params);
        }]
    };
    // output:
    // rootState.resolve Object {}
    
    // case 2:
    rootState.resolve = {
        authInit: ['AuthenticationService', '$stateParams', function (AuthenticationService, $stateParams) {
            console.log('rootState.resolve', $stateParams);
            return AuthenticationService.init($stateParams);
        }]
    };
    // output:
    // rootState.resolve Object {stubCompanyId:...}
    

    Using "angular": "~1.4.0", "angular-ui-router": "~0.2.15"

提交回复
热议问题