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

前端 未结 7 973
隐瞒了意图╮
隐瞒了意图╮ 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:35

    An interesting observation I made while passing previous state params from one route to another is that $stateParams gets hoisted and overwrites the previous route's state params that were passed with the current state params, but using $state.params doesn't.

    When using $stateParams:

    var stateParams        = {};
    stateParams.nextParams = $stateParams; //{item_id:123}
    stateParams.next       = $state.current.name;
    
    $state.go('app.login', stateParams);
    //$stateParams.nextParams on app.login is now:
    //{next:'app.details', nextParams:{next:'app.details'}}
    

    When using $state.params:

    var stateParams        = {};
    stateParams.nextParams = $state.params; //{item_id:123}
    stateParams.next       = $state.current.name;
    
    $state.go('app.login', stateParams);
    //$stateParams.nextParams on app.login is now:
    //{next:'app.details', nextParams:{item_id:123}}
    

提交回复
热议问题