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

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

    EDIT: This answer is correct for version 0.2.10. As @Alexander Vasilyev pointed out it doesn't work in version 0.2.14.

    Another reason to use $state.params is when you need to extract query parameters like this:

    $stateProvider.state('a', {
      url: 'path/:id/:anotherParam/?yetAnotherParam',
      controller: 'ACtrl',
    });
    
    module.controller('ACtrl', function($stateParams, $state) {
      $state.params; // has id, anotherParam, and yetAnotherParam
      $stateParams;  // has id and anotherParam
    }
    

提交回复
热议问题