-
Notifications
You must be signed in to change notification settings - Fork 3k
Disable parameter inheritance in ui-sref directive #376
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
Can you just pass |
Our Application has about 38 states. Every State has its own parameters. One state can have up to 12 parameters. When i pass null to every parameter of every state in every link (ui-sref), there would be very long ui-sref calls. I would be happy if a optional second paramter of type boolean would disable inheritance like this: <a ui-sref="contacts.detail({ id: contact.id }, false)">{{ contact.name }}</a> |
If you define query parmeters (the things after the question mark) then would they be optional in ui-sref? |
Example My current URI: /contacts/teams&sortBy=Name&filterBy=test Now I click on this link: <a ui-sref="attachments.list({ type: 'private' })">list attachments</a> Now my URI: /attachments/list&sortBy=Name&filterBy=test&type=private The query parameters sortBy and filterBy stay in URI until i overwrite it or until i switch to a state which has not defined these parameters in $stateProvider. |
Nothing's simple, is it? :) 2 cents: I'd hope that if parameters that are valid for a state are not specified and they are currently in scope when the ui-sref is invoked, that they retain their values -- and thus, if they are URL parameters, that they stay in the URL. If they are not currently in scope, then they would result in being undefined; that they do not appear in the resulting URL; and that it is the application's responsibility to know what to do about undefined parameters. |
Ok, when you have one controller and the states attachments.list and contacts.teams shares one scope, where the parameters values are stored, your explanation is comprehensible. Inheritance of query parameters is great if parameters were inherited from parent to child state. But inheritance between "master-states" (example above: "contacts.", "attachments.") is confusing. |
I think I agree. I wouldn't consider those to be the same parameters since there isn't an ancestral relationship -- thus they wouldn't be "in scope" when you go from one to the other -- the parameters just happen to have the same names. |
So is there a chance for parameter implementation to disable url-parameter inheritance (between sibling states)? |
@tobigit Could you maybe break down your state tree a little bit, so we can understand better how to craft a solution that's a little bit more optimal for you? I have an idea that I think is a little bit better and more extensible than just adding another parameter (to something that isn't a function anyway), but I want to make sure it'll work for you. |
Sure, here is a snippet of our state configuration: $stateProvider.state('users', {
url: '/:workspaceId/users/?id&sortyBy&filterBy',
controller: 'userController',
templateUrl: '/views/users/index.html'
});
$stateProvider.state('teams', {
url: '/:workspaceId/teams/?id&sortyBy&filterBy',
controller: 'teamController',
templateUrl: '/views/teams/index.html'
}); My currently quick fix is to add some lines in angular-ui-router.js at line 1193. element.bind("click", function(e) {
if ((e.which == 1) && !e.ctrlKey && !e.metaKey && !e.shiftKey) {
// fixes parameter inheritance between sibling states
var fromBase = $state.$current.name.split('.')[0];
var toBase = ref.state.split('.')[0];
var options = {};
if (fromBase != toBase) {
options['inherit'] = false;
}
$state.go(ref.state, params, options);
// ***
scope.$apply();
e.preventDefault();
}
}); |
Okay, should we (@tobigit or me) send a solution as a PR? I do not like the code above because the condition Alternatively (or additionally), we could introduce a new "smart" behavior which recognize state changes and will not apply the current state params? So, the code in question would be something like this: element.bind("click", function(e) {
if ((e.which == 1) && !e.ctrlKey && !e.metaKey && !e.shiftKey) {
var options = {};
// Unless the state in question is a child state of the current one, the params will be not included.
if (!$state.ref.contains($state.$current.name)) {
options['inherit'] = false;
}
$state.go(ref.state, params, options);
scope.$apply();
e.preventDefault();
}
}); |
I added a pull request. Tests of the new regexp: https://www.debuggex.com/r/TuuflzAR_vZ2mWph/1 |
I think we need both:
But I'm not sure @knalli's approach is actually everything we need, because you don't want to check for just if the So @nateabele I'm not entirely sure how inheritance currently works but I'm thinking it's supposed to work like this... say I have some states:
Assuming inheritance is true in all cases... if I go from:
God I know that's a lot to take in... hope you could understand it. So I'm in favor of @tobigit cleaning up his PR (squash and add tests). Then additionally implementing something that auto-inherits in the way I've laid out. |
I am facing this same issue, and built out a plunk before I found this issue, so figured I'd at least include it. It's a very simple case of what @tobigit described (I believe). http://run.plnkr.co/hcaq99zIhV4CO30N/ There are two states, foo and bar, each are basically copies of each other but have different sort values. If you navigate to the Foo state (Foo link in the list at the top) then change the sort, you'll see the "sort" URL parameter change, but then if you click on the Bar link in the list at the top (without sort, second link) you'll be taken to the Bar controller, but the "sort" URL parameter will remain whichever "foo" sort you chose in the Foo controller. Ideally, since the ui-sref is just set to "bar" in this case, and bar is not a child of foo in the state config, the sort shouldn't persist when that ui-sref is clicked. Plunk's source: http://plnkr.co/edit/vZ7rucKQBs8d4oix0I5t Perhaps useful for testing/describing the issue. :) |
@jaydiablo thank you for putting this together. This is a great working example of the issue. |
I also am experiencing this issue. And I concur with timkindberg on how it should behave. |
See |
For reference, the way to do this is:
|
This did not work for me ... My State was this simple : .state('releases', {
parent:'root',
url:'releases/:releaseId',
templateUrl: 'views/view-releases.html',
data: ...
})
When I was using version
I tried Any idea or explanation ? |
I seem to be hitting an issue somewhat similar, but my issue is that params between the same route are being stored. I have a search page which accepts params, and some links to provide access to some predefined searches. The problem I have is that setting Is there a way to disable just the query params and leave the URL params alone? Or at least specify which params to ignore between states/links? |
Is there a way to disable parameter inheritance in ui-sref directive?
I would like to have a second parameter, like this:
With the second parameter i could set the options for the method $state.go().
The text was updated successfully, but these errors were encountered: