Query string parameter in ui-router urls?

2019-01-22 17:50发布

问题:

I have these states:

.state('quotes', {
  abstract: true,
  url: '/quotes'
})
.state('quotes.new', {
  url: '/new',
  templateUrl: 'views/quote-form.html',
  controller: 'QuoteFormCtrl'
})
.state('quotes.new.customer', {
  url: '?customer',
  templateUrl: 'views/quote-form.html',
  controller: 'QuoteFormCtrl'
})

When I hit the URL /quotes/new?customer=123 the ?customer query string is stripped off, and I am left at the quotes.new state.

What would make most sense to me is just adding a params: ['customer'] to the quotes.new state definition, but that gives me an error complaining that I specify both url and params.

Any examples of what I'm trying to do would be much appreciated.

回答1:

You should modify the url string property to include your customer query parameter, like this:

.state('quotes.new', {
  url: '/new?customer',
  templateUrl: 'views/quote-form.html',
  controller: 'QuoteFormCtrl'
});

Multiple parameters can be added by separating them with an &:

.state('mystate', {
  url: '/myState?paramOne&paramTwo
  //...
});

See the docs