Best Practice to share complex data between two pages (ng)


Hi I’m looking for an elegant and solid way to share “complex” data between two pages, i.e. passing a generic object when navigating from page A to page B.

I know it is possible to:

  • serialize it and pass it along the navigationExtras.queryParams property of the router’s navigate method

  • Share an Injectable class between A and B, and let the two pages read/write on the same property

However none of these solutions seems very elegant to me. Serializing is never a very attractive solution, while I have some design concerns about sharing information using an injectable class option (for example, this information will persist in the application even after the specific page transition which is consuming it has been complited. Plus if other components inject such class, they will get access to this data, and the ability to read it and even modify it).

I’m rather looking for something like Android’s context sharing, since differently from a single page web application, in a mobile application - where the Page concept comes into play - is pretty common the need to pass a context when entering a new page.

Any idea?


I might be a little narrow sited since I’m not accustomed to android contexts, but I’ve always injected angular services into page A and page B to share data. Just don’t inject the service into components that you don’t want to access the data?


It is definitely an effective solution, since in the end it allows you to reach the scope. Just… I don’t find it very appealing…
Injectable classes have a specific role in Angular, which is usually to provide application-wide utilities, common actions, set/retrieve common data (e.g. session informations). Navigating from one page to another usually requires an exchange of data that makes sense only for those two pages and is limited to their internal communication protocol. That is a “context”.
I mean, at this point, from a theoretical - and aesthetic - point of view, I would somehow find more elegant to crete one injectable class for each possible A-B pairs of pages - where a page A can navigate to a page B passing parameters… :thinking:


The best thing would have been if NS-NG will expose a context parameter when navigating (ExtendedNavigationExtras), the same thing already exists in NS when doing frame navigation so there should not be any problem.