0

I need to declare an array

and i need to keep each item added to it should remain for there for ever, even if the page is reloaded, is it possible? var arr=[]; arr.push('id');

like this, but stored item should not be replaced.

vishnuprasad kv
  • 990
  • 5
  • 22
  • 46

3 Answers3

3

Go back to the drawing board, that's now how the web works :)

If you reload the page, everything is lost, all the JS code, all the JS variables values, etc. Web is stateless by design.

There are ways to achieve this: either store in the server and load it back on every page request or browser-based techniques which require a bit more knowledge since they are a bit more advanced.

About browser-based (client side) techniques: one way I can think of is storing your data in cookies. You can read it up here. Cookies are kept on your browser and are kept alive for your browsing sessions. Still, it's a very fragile method: people can easily delete them or even block them all together.

A better way to do it is HTML5 Local storage API. Try to read into that here or here or google it. People can still delete it and it's really only attached to your individual browser (change computers? you lost it, it's locally saved)

Anyway, you have to think twice and really try to understand how server + client works and where to keep things. If that data is very important, it should be kept on the server, that's the only way to ensure it's safe and saved.

AlfaTeK
  • 7,487
  • 14
  • 49
  • 90
  • There is quite some technical ways to do this, He must think first about the fonctionnality that he wants to implements with this system. @VIs Prasad, if you're lost in this, tell us what functionnality are your trying to implement, so we can guide you to which technical solution fits to your problem. – Walfrat Feb 18 '16 at 12:56
1

Every angular application has a single root scope. All other scopes are descendant scopes of the root scope.

So one option would be to do it like

angular.module('test', []).run(function($rootScope)
{        
    $rootScope.myArray = [];
    $rootScope.myArray.push("test value");
});

However having said that use of $rootScope is not recommended so you can use value provider in angular.

app.value("testArray", {
    someArray : []
});

then inject it in controller

app.controller("myController", function ($scope, testArray) {
    $scope.someValue = testArray.someArray;
});
Navoneel Talukdar
  • 4,393
  • 5
  • 21
  • 42
0

You can use cookies in AngularJS to maintain the values. FYI, enter link description herea JSFiddle of this using the $cookieStore, two controllers, a $rootScope, and AngularjS 1.0.6. It's on JSFiddle as https://jsfiddle.net/h2fs0han/ as a base if you're messing around with this...

For more information refer - How to access cookies in AngularJS?

<div ng-app="myApp">
  <div id="lastVal" ng-controller="ShowerCtrl">{{lastVal}}</div>
  <div id="button-holder" ng-controller="CookieCtrl">
    <input type="text" ng-model="strText" />
    <button ng-click="AddItem()">Add Item!</button>
  </div>
</div>

 var myApp = angular.module('myApp', ['ngCookies']);
 myApp.controller('CookieCtrl', function($scope, $rootScope, $cookieStore) {
  $scope.strText = "";

  $scope.AddItem = function() {
    var lastVal = $cookieStore.get('lastValue');
    if ($scope.strText) {
      if (!lastVal) {
        $rootScope.lastVal = new Array(); //Default
        $rootScope.lastVal.push($scope.strText);
      } else {
        var newItem = angular.copy(lastVal);
        newItem.push($scope.strText);
        $rootScope.lastVal = newItem;
      }
      $cookieStore.put('lastValue', $rootScope.lastVal);
    }
  };
});

myApp.controller('ShowerCtrl', function() {});
Community
  • 1
  • 1
Sumit Deshpande
  • 2,155
  • 2
  • 20
  • 36