In AngularJs is there any way to share object between two web pages without using $cookies. For example my Login page controller have different angular.module and my other controller have different angular.module but I need to share credentials between Login page and another page without using $cookies.
-
The question is, why do you no longer want use $cookies ? – sebastienbarbier Apr 15 '15 at 12:08
3 Answers
Between independent page
You can use the standard HTML5 sessionStorage and localStorage object
For simple synchronous storage access HTML 5 introduces the localStorage attribute on the Window object: localStorage["status"] = "Idling.";
LocalStorage is like cookie, sessionStorage will be clean when closing your browser.
In angular
You can use a factory which is technically a singleton. But if you refresh your page, all JS will be re-initialize and you will lose your data. A Service is also possible.
Here is a link on an other topic explaining difference between Services and Factory : AngularJS: Service vs provider vs factory
To create Services/Factory, give a look at Angular official documentation, it is well explained.
Perfect mix
What you need to do is create a Service, and at each modification you stringify it to store on a local/session Storage. On load, when angular create your service, you look in your storage for initialization value and your object is back.
This is quite common for authentification for exemple. You store a token to keep authentification when refreshing ;). Let me know if any difficulty to implement.

- 1
- 1

- 6,542
- 3
- 29
- 55
Have you considered creating a Service which stores these login credentials? You could then use Dependency Injection to have this Service available in both controllers.

- 3,534
- 10
- 51
- 105
-
1Yes, but the problem in this is when current page is changed, the browser is automatically refresh and value of that object is lost. – Rajshree Soni Apr 15 '15 at 12:04
-
True, unless this service is setup to perform CRUD operations on a database. Sounds like this is more functionality than you actually are aiming for though. – MattDionis Apr 15 '15 at 12:06
Sharing the information between different pages is a critical task which can be done with the help of Controllers.
In AngularJS, we can share the data between controllers in three ways:
Factory: Object is created inside the factory and return it . Service: With the service, you just have a standard function that uses the this keyword to define function. Provider: With the provider, there’s a $get you define and it can be used to get the object that returns the data.

- 1,109
- 8
- 16