0

I am trying to develop 2 pages:

Page 1: Normal Login Page login.html Page 2: Home Page home.html

I am using AngularJS to code the scripts in these pages.

I have to transfer some data between these two pages. So i decided to use cookies (I tried to use Services but i didn't know how to write the same service for 2 separate files).

In the Login page i saved the cookies as follow :

    var resultObj = {'loginid',result};
    $cookies.dotobject = resultObj;
    window.location = 'Home.html';

I checked that this cookie is saved correctly (Console.log(resultObj))

However when i open the home page i try to read this cookie using:

   var result = $cookies.dotobject;
   console.log(result) //To check if it worked

But it didn't, i got undefined.

My question is can I use $cookies in this case ? And why does it disappear ?

If cookies are not useful in this case how to use a service between 2 different pages containing 2 different controllers ?

Excuse me if this questions aren't from a good quality since i am still learning these concepts.

EDIT

I tried to write in the same document :

    console.log(result);
    $cookies.put('loginid',result);
    var loginida = $cookies.get('loginid');
    console.log(loginida);

But the $cookies.get does not work ; It gives also undefined . Any ideas ?

  • That doesn't appear to be the correct way to use `$cookies` https://docs.angularjs.org/api/ngCookies/service/$cookies – Matthew Green Feb 26 '16 at 21:22
  • I used the code suggested in http://stackoverflow.com/questions/10961963/how-to-access-cookies-in-angularjs , i will check the documentation now and try again – user3179490 Feb 26 '16 at 21:23
  • what version of angular? Are you working with object? Using service or localStorage would be better if so – charlietfl Feb 26 '16 at 21:40
  • In the first example i was using angular 1.2.19 (as suggested in the link) In the second i was using 1.4.8 (as in the documentation) – user3179490 Feb 26 '16 at 21:41
  • To solve the problem i used localStorage as charlietfl suggested, and it works exactly as needed. Thanks – user3179490 Feb 26 '16 at 22:32

0 Answers0