way to create optional parameter in url?
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'
]
}
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 Thanks. Your solution will have to do for now.
@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.
+1 support
url: '/:username[/:page[/:question]]'
I'm sure I've seen that syntax in some router framework (can't remember).
+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.
You can already do query params. @ksperling are query params optional?
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}'
But what if you do the URL like this? url: '/add?groupId'
Then navigate to either /add or /add?groupId=123
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'
Thanks, that worked! I did try it earlier but got exception from ui.router. I must have done something wrong.
+1 to add optional paramaters like item/{:id?}
+1
+1
Upvote :+1:
url: '/:username[/:page[/:question]]',
defaultParams: {page: 1}
+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 Since it's obviously top priority for you, you're welcome to work on it and submit a patch.
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 .
@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
I like the syntax :) If you need any help, let me know.
@brian-frichette Thanks! I appreciate it. I'm picking up work from where I stopped this weekend, so I'll keep you posted :)