Parameters for states without URLs in ui-router for AngularJS

前端 未结 3 1674
梦谈多话
梦谈多话 2020-12-28 12:31

I am using ui-router to represent states in my AngularJS app. In it I\'d like to change the state without changing the URL (basically a \"detail view\" is updated but this s

相关标签:
3条回答
  • 2020-12-28 13:07

    Thanks for your answer, it did help me in the right direction but I'd just like to add a more complete description.

    In my specific issue there was a complicating factor because the state I needed to inject a non-URL parameter to was a child state. That complicated things slightly.

    The params: ['id'] part goes in the $stateProvider declaration like this:

    $stateProvider.state('parent', {
        url: '/:parentParam',
        templateUrl: '...',
        controller: '...'
    }).
    state('parent.child', {
        params: ['parentParam','childParam'],
        templateUrl: '...',
        controller: '...'
    });
    

    And the param name is connected to the ui-sref attribute like this:

    <a ui-sref=".child({ childParam: 'foo' })">
    

    And the catch is this:

    If the parent state also has a URL parameter then the child needs to also declare that in its params array. In the example above "parentParam" must be included in the childstate.

    If you don't do that then a module-error will be thrown when the application is initialized. This is at least true on the latest version at the time of writing (v.0.2.10).

    EDIT

    @gulsahkandemir points out that

    Declaration of params in a state definition has changed to params: { id: {} } from params: ['id']

    Judging by the changelog, this seems to be the case starting from v0.2.11

    Details of params can be found in the official docs

    0 讨论(0)
  • 2020-12-28 13:08

    Just an additional information for new comers to this post:

    Declaration of params in a state definition has changed to params: { id: {} } from params: ['id']

    So be aware :)

    Source: http://angular-ui.github.io/ui-router/site/#/api/ui.router.state.$stateProvider

    0 讨论(0)
  • 2020-12-28 13:10

    I now figured out, that you need to use the params: ['id'] property of the state in order to have the key not stripped when not using a URL.

    0 讨论(0)
提交回复
热议问题