-
Notifications
You must be signed in to change notification settings - Fork 3k
way to create optional parameter in url? #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
You can use a custom regex for the parameter if you use curly brace (JAX-RS style) sytax:
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
I would like to pass few optional param with the url.
So a user can visit the |
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
|
But what if you do the URL like this? 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
|
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 similar comment
+1 |
Upvote 👍
|
+1 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 :)
|
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 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:
In this example, if So far what I did is attach the I'm going with the syntax suggested by @xixixao above, so next I will be adding the parsing of those square brackets to the To access the 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 :) |
👍 |
+1 It would be nice if the optional flag was the same as angular-router ( |
Any update on this issue? +1 to any of those syntaxes. |
I think as a start it should be fairly easy to support I suppose the (zend-like) |
@diogobeda Was very occupied in the past month, and this task was pushed back, but according to my schedule I will focus on this towards the end of next week. That means Wednesday or so. |
Rooting for you @stereokai. Viel Erfolg! |
I'm a bit late to the party here, and sorry if I missed the answer to this somewhere, but is the trailing slash required? I have: .state('predictions', {
url: '/predictions/:id/:slug',
templateUrl: 'views/predictions.html',
controller: 'PredictionsCtrl'
}) I can leave off my last param of slug, but I must still have the slash then. Is there a way to make that slash optional? |
I found this when running into the problem of double slashes. Making parameters optional using the method described in #1032 does NOT resolve this issue: $stateProvider.state('tutorials.view', {
reloadOnSearch: false,
url: '/:tutorialAlias/:lessonAlias/:lessonSlide',
controller: 'TutorialsViewCtrl',
templateUrl: 'tutorials/view/view.html',
params: {
lessonAlias: {value: null},
lessonSlide: {value: null}
}
}); Then, when using ui-sref: <a ui-sref="tutorials.view({tutorialAlias: tutorial.alias})">Start tutorial</a> The link that gets created has a double trailing slash because of the missing lesson alias (e.g. some/url/tutorial-alias// ). Can't seem to find a way to fix this. Any thoughts? Using 0.2.13. Edit: found a workaround by defining a mock-child state: //State definition
$stateProvider.state('tutorials.view', {
url: '/:tutorialAlias',
controller: 'TutorialsViewCtrl',
templateUrl: 'tutorials/view/view.html'
})
//With lesson
.state('tutorials.view.lesson', {
reloadOnSearch: false,
url: '/:lessonAlias/:lessonSlide',
controller: 'TutorialsViewCtrl',
templateUrl: 'tutorials/view/view.html'
}); But I still think the url generator should be "clever" enough to strip out double slashes when parameters are missing. |
+1 |
1 similar comment
+1 |
+1 I need support for an optional trailing slash |
Same here |
+1 |
Hey guys, I just found the amazing squash parameter for the params optionnal object in ui-router. So if we take the @AdamBuczynski example :
So it will produce url like if we only have lessonAlias: Another exemple here with no lessonAlias and a custom lessonSlide value
So it will produce url like: I think the squash param is a way to avoid double slashes when parameters are missing. |
@juli3773 Thanks! I'll try that out tonight |
@rwwagner90 tell me if it's working ^^ |
For everyone 👍 this issue, this is already released—you don't need to vote for it anymore. I'm using it in one of my projects and it's working. |
@jshado1 could you link the docs for this feature. I couldn't find it. Which one of the notations is implemented? |
@juli3773 sorry for the late response, but it does work. Thanks! |
So, now I have a new problem. How can I make a parameter required? I have the following: .state('profile', {
url: '/profile/:id/:slug',
templateUrl: 'views/profile.html',
controller: 'ProfileCtrl',
params: {
slug: {value: null, squash: true}
}
}) I need /profile to not be valid though. I want only /profile/id to work |
What behavior do you want on /profile ? |
@juli3773 Essentially, what I could not do before was: id is a required param and slug is not. I need the following to all be valid: /profile/id /profile/id/ /profile/id/slug /profile/id/slug/ And these should be invalid: /profile /profile/ |
Hey guys, I am stuck on this dynamic parameter passing to my controller, my state is look like this, controller: But I am not getting proper output on this. Please any one give me solution..!! |
@muthu07 NB, www.stackoverflow.com might be a better place for those kinds of questions, seeing as it does not seem to relate to this issue. |
@JohannesFerner as well as anyone else. Then there's the comments from the squash pull, where the syntax seems to have changed ever so slightly #1501 from I managed to get it work with the new syntax. Following the example above, that would be It would be really neat if the official wiki could be updated - by someone in the know - with the current implementation as per the latest release 0.2.13 |
Please disregard my previous comment, the documentation is here the And I just don't seem the get optional params to work as expected, which is also unrelated to the otherwise clause that |
+1 |
Alright, closing this out. @rwwagner90 if you have a different problem, please post that in a new issue so I don't have to read a small novel to figure the status of it. 😉 🍻 |
@juli3773 thanks a lot |
+1 |
I want to have optional integer-type parameter like this: |
@slavafomin Yes it's possible! So you'll have: url: '/sign-up/{partnerId:int}',
params: {
partnerId: {value: null, squash: true}
} |
Thank you @juli3773, I've finally managed to find a similar solution. Is params: {
partnerId: null
} And it seems to work great ) |
@slavafomin the |
I tried but it won't match /s/ and then auto populate the rest of the params with default value. It only works if I populate all params but the last one, then it will work but now show the default value in the URL. Any ideas on why @okendoken ? |
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:
The text was updated successfully, but these errors were encountered: