ui-router icon indicating copy to clipboard operation
ui-router copied to clipboard

way to create optional parameter in url?

Open legomind opened this issue 13 years ago • 126 comments

I fiddled with some regex, but this is beyond me.

I need the last parameter in my state to be optional. In other words: capture it if it is there, but otherwise ignore it.

Something like:

$stateProvider
.state \user.page, {
  url: '/:username/:page?'
  template-url: '...'
  controller: [
  \$stateParams
  ($stateParams) ->
    # The page parameter should be optional.
    # This should activate on both '/legomind/dashboard' as well as '/legomind'
  ]
}

legomind avatar Apr 28 '13 22:04 legomind

You can use a custom regex for the parameter if you use curly brace (JAX-RS style) sytax:

url: '/{username}{page:(?:/[^/]+)?}'

Note that in this case the '/' preceding the optional segment is going to be part of the 'page' value. We could look at allowing capture of a specific part of the regexp via capturing parenthesis, but this would require pretty much completely pre-parsing the regexp to see whether it has capturing parenthesis (as opposed to non-capturing or other uses of the '(' and ')' characters).

ksperling avatar May 02 '13 07:05 ksperling

@ksperling Thanks. Your solution will have to do for now.

legomind avatar May 02 '13 19:05 legomind

@legomind Another idea to solve the same issue: Use $urlRouterProvider to do a redirect from e.g. '/{username}' to '/{username}/1' or whatever your default page is, then the state.url only needs to handle the case where the parameter is present.

ksperling avatar May 05 '13 20:05 ksperling

+1 support

  url: '/:username[/:page[/:question]]'

I'm sure I've seen that syntax in some router framework (can't remember).

xixixao avatar Jun 09 '13 20:06 xixixao

+1 support as well

 .state('user.add', {
    url: '/add',
    templateUrl: 'app/user/user.add.html',
    controller: 'AddUserController'
  })

I would like to pass few optional param with the url.

 users/add?groupId=12&orgId=2

So a user can visit the users/add directly or from another controller I can transition to user.add state with those param values.

amitava82 avatar Jun 23 '13 16:06 amitava82

You can already do query params. @ksperling are query params optional?

timkindberg avatar Jun 23 '13 18:06 timkindberg

I'm afraid it is not unless I'm doing something wrong. If I don't pass groupId to url then it hits my otherwise route.

url: '/add/{groupId}'

amitava82 avatar Jun 23 '13 18:06 amitava82

But what if you do the URL like this? url: '/add?groupId'

Then navigate to either /add or /add?groupId=123

timkindberg avatar Jun 23 '13 19:06 timkindberg

yes, query parameters are optional. Like @timkindberg shows in his example, you still have to declare them in the URL pattern though, so that $stateProviders knows which parameters you're interested in, i.e. your URL pattern should be something like

url: 'users/add?groupId&orgId'

ksperling avatar Jun 24 '13 00:06 ksperling

Thanks, that worked! I did try it earlier but got exception from ui.router. I must have done something wrong.

amitava82 avatar Jun 24 '13 01:06 amitava82

+1 to add optional paramaters like item/{:id?}

benjamingeorge avatar Dec 09 '13 16:12 benjamingeorge

+1

tamtakoe avatar Dec 18 '13 10:12 tamtakoe

+1

fakingfantastic avatar Dec 19 '13 23:12 fakingfantastic

Upvote :+1:

url: '/:username[/:page[/:question]]',
defaultParams: {page: 1}

miraage avatar Dec 21 '13 16:12 miraage

+1 This should be top priority, not 0.4 milestone

One way to get around it, is to check first if the value given to the optional parameter is not the name of a child state.

If it is, then the URL should be treated like it points to the child state, skipping the optional parameter on the parent.

stereokai avatar Dec 31 '13 13:12 stereokai

@stereokai Since it's obviously top priority for you, you're welcome to work on it and submit a patch.

nateabele avatar Jan 01 '14 00:01 nateabele

I am already working on one, using the $stateProvider decorator :) I'll post it here when I'm done On Jan 1, 2014 2:10 AM, "Nate Abele" [email protected] wrote:

@stereokai https://github.com/stereokai Since it's obviously top priority for you, you're welcome to work on it and submit a patch.

— Reply to this email directly or view it on GitHubhttps://github.com/angular-ui/ui-router/issues/108#issuecomment-31415793 .

stereokai avatar Jan 02 '14 07:01 stereokai

@nateabele @ksperling

A small update:

I have just spent 3 hours with the UI-Router code - which was a stimulating and intriguing journey. I have dug into the depths of $UrlRouterProvider and I now understand the relationship between it and the UrlMatchers, as well as how UI-Router applies this relationship when handling location changes and state transitions. Pretty effing awesome code there, guys.

Okay, so as I mentioned above, one way I can see which makes optional parameters possible, is checking if the value of a parameter is in fact the URL segment of a child state. So, to give an example:

    Parent state URL: /parent[/:optionalParam]
    Child state URL: /child

In this example, if optionalParam holds the value "child", it can't be captured and the transition should be delegated to the next rule, i.e. the child state.

So far what I did is attach the UrlMatchers to every rule, just so I can access them from within UrlMatcher.exec.

I'm going with the syntax suggested by @xixixao above, so next I will be adding the parsing of those square brackets to the placeholder regex. Regular expressions have never been my strong side, so for now, I won't be taking care of nested optional parameters.

To access the UrlMatchers from the exec function, I also had to define the rules array outside of $UrlRouterProvider. It's only 1 closure jump, so it's not so bad. However, that strategy might still suck - I haven't yet thought that one out well enough, but executing this check on all child states might pose a significant impact on performance, as it could lead to loops over loops on the rules array and the objects within. Perhaps building a hierarchy object, optimized for recursion, in the UrlMatcher constructor's while loop, is a better direction.

Another way to optimize is to set up a flag on rules which have optional parameters, so rules without them will completely skip on looking for clashes with state paths and spare some precious CPU cycles.

Okay, that's it so far. I'll update next week as I progress. Sorry if I don't make sense, I'm not a native English speaker

stereokai avatar Jan 04 '14 18:01 stereokai

I like the syntax :) If you need any help, let me know.

bfricka avatar Jan 14 '14 19:01 bfricka

@brian-frichette Thanks! I appreciate it. I'm picking up work from where I stopped this weekend, so I'll keep you posted :)

stereokai avatar Jan 16 '14 18:01 stereokai