You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Reacted by Michaël Zwyssig, Schmitt Christian, Abdurrahman, Shahinur Islam, Lars, Maxime, Duc Le, ChayaChechik, zer09, Vishnudev Krishnadas and 23 more
It is funny because i was just looking at this today. The only way i could see to pass data was via route which is static in the router config ... so i created this hacked solution:
This works the only issue i saw is if you refresh your screen it does not persist the data.
Reacted by Terary Chambers, Mihir Patel, Cristian Morataya, alexhildebrand, Rafael Smith, Tim Schmidt, logicalaandoni, Vishnudev Krishnadas, Mohamed Abd ELmawgoud, Deyan Garvanski and 3 more
@earshinov@tmburnell I like the idea of passing arbitrary data, but it think they should be passed as route params and resolved via resolvers.
For example, You want to go to page with item details but You already have the item object, so You navigate to /item/:id and pass it as param. Then in resolver You could use the object in params to resolve data or use id param to download it or navigate to error page. The thing is, You should take care of situation when user clicks refresh button in browser and all the params that are not in url will be lost.
EDIT: I think the most common use case for this would be forms. You could easily create multi page forms or use it to fill forms with data You already have, like when You navigate from item details to item modification form.
@dawidgarus I also wanted to try and use a URL state (that would allow for refresh)... but could not find API's that would allow this to work.
The I had for not putting it in the URL is for something like E-commerce (or something restrictive) I dont want to make it deep linkable. I want to force a user to come from somewhere (within the app) not just click a link.
Guys, for those of you who are looking for ways to persist the state, please follow #10248. That task is already in progress and hopefully you will have this feature available soon.
@earshinov I don't think that history state is a way to go. There is one state for all pages and it's not preserved if You navigate to a subpage. For example, You go to /foo and set some data in history state that could be retrieved by FooComponent attached to route /foo. Then You go to /foo/bar and then to /baz. When You go back to /foo/bar, FooComponent can't retrieve the data. That's why the data should be attached to angular's route.
@dawidgarus I might be mistaken, but there is a widely accepted solution for your scenario--storing the state in a service provided in the /foo component.
@dawidgarus, In that case I would provide the service in the root component module then.
@michaelz, The comment you referenced is about persisting the data—a topic I would prefer to be discussed solely in #10248 and not brought up in this ticket.
I understand that I might be missing the whole picture, but on the other hand I'd like to have this ticket neet, tidy, understandable and focused on the one fairly simple scenario I've given in the ticket description.
@earshinov Another bad idea. You could return to previous page, but when You navigate to the same page all the information will be reset, so You can't go back: /foo (saved data for this page in root service is lost) -> /bar -> /foo
Thanks for the issue all. See the PR #27198 allowing you to set history.state on navigation. This state object will be available to the currently executing navigation. We are finalizing how this API will look, but this should land very shortly and hopefully will satisfy what you're looking for.
If you have comments on the PR, please add them there rather than on this issue as I will see them quicker on the PR.
@jasonaden the solution with history state doesn't work well with nested and auxiliary routes as I described above - you would need to manually merge the state at each navigation. Also, there's a limit on history state size and you can only pass json objects that can be serialized, which greatly decrease it's usefulness for scenarios above.
@jasonaden , I share @dawidgarus's opinion that the fix does not cover all cases. The case I had in mind when I was filing the ticket (perhaps, different from @dawidgarus's) is as follows:
There is a navigable component A fetching some data from the backend.
There is a second component B that sometimes navigates to A and happen to have all the data A need.
If there was a way to pass data with router.navigate, B could pass the data to A so that A does not need to make an excessive request to the backend.
Note that in my case passing additional data serves only optimization purposes. It does not cancel the fact that A must be able to fetch the data itself when the user navigates to it directly rather than via component B.
As I pointed out in #27198, as a user I would greatly appreciate some documentation regarding the lifecycle of a single navigation as it's unclear to me "how long" a navigation is on-going and thus how long I can access this data.
I don't understand, why some solutions (which were available earlier) need to be proved that are needed... I know that some things can be made other way, but what for, when such solution was the best one...
Tooday I just needed exactly such case when I wanted to use such option. Because I use data in routing definition, where set some const params, aadditionally some data set be resolever, and neede add extra data in case when someone is using for navigation specific buttons, and this is the perfect example, where passing data by extending snapshot data params would be the best way... and I can't, becouse..?
I have to install additionall solutions tu support functionalities which has been removed from previous version of framework...?
I actually didn't read them, sorry. I just randomly came across this issue when looking up something else.
The case you described sounds like a basic state management task to me. Either have a service that lazily fetches the data or use a more sophisticated tool like ngrx. Why doesn't that work?
In any case if this is still an open question then of course the core team should respond to it first.
It works, but making a separate service with the sole purpose of being a temporary state holder / transfer-er feels an awkward and unnecessary complication.
Just so you know, you are not the first to suggest services, see tickets I linked in the description.
I would argue that passing data around in the navigation everywhere makes for a complex, hard to reason with architecture that won't scale.
But I'm not really here to argue any of that. I randomly stumbled across this issue and thought to point out that it appeared to be closeable. I was wrong about that, I didn't mean to undermine remaining open questions.
Activity
tmburnell commentedon Aug 24, 2018
It is funny because i was just looking at this today. The only way i could see to pass data was via route which is static in the router config ... so i created this hacked solution:
This works the only issue i saw is if you refresh your screen it does not persist the data.
dawidgarus commentedon Aug 25, 2018
@earshinov @tmburnell I like the idea of passing arbitrary data, but it think they should be passed as route params and resolved via resolvers.
For example, You want to go to page with item details but You already have the item object, so You navigate to /item/:id and pass it as param. Then in resolver You could use the object in params to resolve data or use id param to download it or navigate to error page. The thing is, You should take care of situation when user clicks refresh button in browser and all the params that are not in url will be lost.
EDIT: I think the most common use case for this would be forms. You could easily create multi page forms or use it to fill forms with data You already have, like when You navigate from item details to item modification form.
tmburnell commentedon Aug 27, 2018
@dawidgarus I also wanted to try and use a URL state (that would allow for refresh)... but could not find API's that would allow this to work.
The I had for not putting it in the URL is for something like E-commerce (or something restrictive) I dont want to make it deep linkable. I want to force a user to come from somewhere (within the app) not just click a link.
earshinov commentedon Aug 29, 2018
Guys, for those of you who are looking for ways to persist the state, please follow #10248. That task is already in progress and hopefully you will have this feature available soon.
dawidgarus commentedon Aug 29, 2018
@earshinov I don't think that history state is a way to go. There is one state for all pages and it's not preserved if You navigate to a subpage. For example, You go to /foo and set some data in history state that could be retrieved by FooComponent attached to route /foo. Then You go to /foo/bar and then to /baz. When You go back to /foo/bar, FooComponent can't retrieve the data. That's why the data should be attached to angular's route.
earshinov commentedon Aug 29, 2018
@dawidgarus I might be mistaken, but there is a widely accepted solution for your scenario--storing the state in a service provided in the /foo component.
dawidgarus commentedon Aug 29, 2018
@earshinov That won't for the scenario from my previous comment. Service will be destroyed when You navigate from /foo/bar to /baz
michaelz commentedon Aug 30, 2018
@earshinov you may want to have a look at this comment #10248 (comment)
earshinov commentedon Aug 30, 2018
@dawidgarus, In that case I would provide the service in the root
componentmodule then.@michaelz, The comment you referenced is about persisting the data—a topic I would prefer to be discussed solely in #10248 and not brought up in this ticket.
I understand that I might be missing the whole picture, but on the other hand I'd like to have this ticket neet, tidy, understandable and focused on the one fairly simple scenario I've given in the ticket description.
dawidgarus commentedon Aug 31, 2018
@earshinov Another bad idea. You could return to previous page, but when You navigate to the same page all the information will be reset, so You can't go back: /foo (saved data for this page in root service is lost) -> /bar -> /foo
andreaslarssen commentedon Sep 5, 2018
+1
jasonaden commentedon Nov 20, 2018
@earshinov @dawidgarus @andreaslarssen @tmburnell @michaelz
Thanks for the issue all. See the PR #27198 allowing you to set
history.stateon navigation. This state object will be available to the currently executing navigation. We are finalizing how this API will look, but this should land very shortly and hopefully will satisfy what you're looking for.If you have comments on the PR, please add them there rather than on this issue as I will see them quicker on the PR.
4 remaining items
dawidgarus commentedon Dec 4, 2018
@jasonaden the solution with history state doesn't work well with nested and auxiliary routes as I described above - you would need to manually merge the state at each navigation. Also, there's a limit on history state size and you can only pass json objects that can be serialized, which greatly decrease it's usefulness for scenarios above.
earshinov commentedon Dec 4, 2018
@jasonaden , I share @dawidgarus's opinion that the fix does not cover all cases. The case I had in mind when I was filing the ticket (perhaps, different from @dawidgarus's) is as follows:
router.navigate, B could pass the data to A so that A does not need to make an excessive request to the backend.Note that in my case passing additional data serves only optimization purposes. It does not cancel the fact that A must be able to fetch the data itself when the user navigates to it directly rather than via component B.
We are currently using https://github.com/Hipparch/Angular2-navigate-with-data.
Airblader commentedon Dec 16, 2018
As I pointed out in #27198, as a user I would greatly appreciate some documentation regarding the lifecycle of a single navigation as it's unclear to me "how long" a navigation is on-going and thus how long I can access this data.
From a quick test it seems that
contains the state, but is this guaranteed? When does
queryParamMapemit?RamItDown commentedon Jan 21, 2019
I don't understand, why some solutions (which were available earlier) need to be proved that are needed... I know that some things can be made other way, but what for, when such solution was the best one...
Tooday I just needed exactly such case when I wanted to use such option. Because I use data in routing definition, where set some const params, aadditionally some data set be resolever, and neede add extra data in case when someone is using for navigation specific buttons, and this is the perfect example, where passing data by extending snapshot data params would be the best way... and I can't, becouse..?
I have to install additionall solutions tu support functionalities which has been removed from previous version of framework...?
Airblader commentedon Feb 2, 2019
@jasonaden I think this issue can be closed now since passing state landed in 7.2.0?
earshinov commentedon Feb 3, 2019
@Airblader , did you care to read my and @dawidgarus's comments?
Airblader commentedon Feb 3, 2019
I actually didn't read them, sorry. I just randomly came across this issue when looking up something else.
The case you described sounds like a basic state management task to me. Either have a service that lazily fetches the data or use a more sophisticated tool like ngrx. Why doesn't that work?
In any case if this is still an open question then of course the core team should respond to it first.
earshinov commentedon Feb 3, 2019
It works, but making a separate service with the sole purpose of being a temporary state holder / transfer-er feels an awkward and unnecessary complication.
Just so you know, you are not the first to suggest services, see tickets I linked in the description.
Airblader commentedon Feb 3, 2019
I would argue that passing data around in the navigation everywhere makes for a complex, hard to reason with architecture that won't scale.
But I'm not really here to argue any of that. I randomly stumbled across this issue and thought to point out that it appeared to be closeable. I was wrong about that, I didn't mean to undermine remaining open questions.
atscott commentedon Apr 26, 2024
This was actually resolved by 5c1d441. You can access the info in several ways:
router.getCurrentNavigation().extras.inforouter.lastSuccessfulNavigation.extras.infoActivatedRoute.dataif you get the information in a resolverangular-automatic-lock-bot commentedon May 27, 2024
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.