I have two html pages. First page being a list of patients each being a hyperlink. The expected behavior is that, on click of a hyperlink ( corresponding to a patient), the user should be redirected to another html page which has the details of the corresponding patient. The redirection is happening. But, I am not able to pass the data from the first html page to the second one.
-
Post some code. Are you using 2 controllers and 2 templates, 1 controller and 2 templates, do you have a directive setup for displaying the data? Too many questions to give an answer.mikeb– mikeb2016-03-30 11:26:31 +00:00Commented Mar 30, 2016 at 11:26
-
Possible duplicate of Angularjs: build url with query stringBonatti– Bonatti2016-03-30 11:31:39 +00:00Commented Mar 30, 2016 at 11:31
-
U need to pass object or string to another page ?rejo– rejo2016-03-30 11:34:39 +00:00Commented Mar 30, 2016 at 11:34
-
I have two Controllers. Using Controller1, I'm able to display the list of patients(by performing a get operation). Each link(link being a patient) has a unique patient id. I have a function such that on click of the link, it sends the patiend id to the controller(Controller1). Making use of $broadcast and $on , I'm able to send that patient id to Controller2. I'm even able to display the Patient id selected(clicked) from Controller2. BUT I can do this only in page1.html(i.e. below the list of patients, without directing to page2.html). But what I ideally want is to send the pat id to page2.Ayush– Ayush2016-03-30 11:38:23 +00:00Commented Mar 30, 2016 at 11:38
-
Possible duplicate of Angularjs pass data in between services that exist on different pagescatzilla– catzilla2016-04-01 09:21:44 +00:00Commented Apr 1, 2016 at 9:21
|
Show 1 more comment
2 Answers
There are plenty of way to do that:
- localStorage of the browser (angular plugin)
- URL parameters (angular $location)
- Server-side session
- Cookies, outdated and inefficient nowadays, better go for #1 (using cookies with angular)
Best applicable implementation depends on your architecture on front/back
Comments
The URL should contain an ID for the item you want to display, e.g. /patient/432.
Using that ID, the second page loads the data.
If you are using Angular, and both pages are in fact part of a single app, you would use a Service that caches the data. That is, the service loads your patients list and returns either a list or a single item from the list. That way, you don't have to load the individual items from the server API each time.
3 Comments
Ayush
Thank You C14L, I used a service to share the data between two pages. But the problem now is I'm not able to share a data which is inside a function of controller1 to Controller2. If you could help me out with this?
C14L
Does the controller1 change the data? It should call a method on your service to do that. And that method changes the data in the service, that is then accessible to all controllers. Alternatively, you could store the data as a property on $rootScope and change it there, then your controller2 could access to too.
Ayush
No Controller1 doesn't change the data. It just picks up the argument(i.e.Pat Id), and I'm kinda lost with $rootScope example which you have mentioned. Could You please elaborate it? Thanks in Advance:)