0

i have one doubt how can i pass data from one page to another page in angularjs without using localstorage and data should exist even if user reload that page.

Is there any way out from here or not?

Thank's in advance

Lalan
  • 1
  • 1

3 Answers3

0

you can use cookie's :

https://docs.angularjs.org/api/ngCookies/service/$cookies

you can refer to this JSbin link for example:

http://jsbin.com/duxaqa/2/edit

Also, there is a nice stackoverflow answer that match your needs i think, with a more complete explanation than mine:

How to access cookies in AngularJS?

Community
  • 1
  • 1
erwan
  • 887
  • 8
  • 17
0

You can put data to cookie

// set cookie on one page:
setCookie("key", value);

// get cookie on another page:
var val = getCookie("key");

Using functions:

function setCookie(name, value, options) {
  options = options || {};

  var expires = options.expires;

  if (typeof expires == "number" && expires) {
    var d = new Date();
    d.setTime(d.getTime() + expires * 1000);
    expires = options.expires = d;
  }
  if (expires && expires.toUTCString) {
    options.expires = expires.toUTCString();
  }

  value = encodeURIComponent(value);

  var updatedCookie = name + "=" + value;

  for (var propName in options) {
    updatedCookie += "; " + propName;
    var propValue = options[propName];
    if (propValue !== true) {
      updatedCookie += "=" + propValue;
    }
  }

  document.cookie = updatedCookie;
}

function getCookie(name) {
  var matches = document.cookie.match(new RegExp(
    "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
  ));
  return matches ? decodeURIComponent(matches[1]) : undefined;
}

But keep in mind that cookies size limited: 4093 bytes per domain.

Constantine
  • 482
  • 3
  • 10
0

you can pass data to another page using $rootScope or $cookieStore. But, only $cookieStore will save data when user reload the page. Take a look how to implement example:

angular.module('cookieStoreExample')
.controller('ExampleController', function($cookieStore) {
  // Put cookie
  $cookieStore.put('myFavorite','oatmeal');
  // Get cookie
  var favoriteCookie = $cookieStore.get('myFavorite');
  // Removing a cookie
  $cookieStore.remove('myFavorite');
});
Wesin Alves
  • 371
  • 1
  • 3
  • 13