4

visit https://github.com/gsklee/ngStorage.

My code has 2 partials, in partial1 I've 3 input box and data entered in them be 'abc', 'pqr', 'xyz' and on click of button I want to get redirected to partial2 where input box gets populated with following details calculated in a controller 'abcpqr', 'abxy'.

Both partials uses localStorage [ngStorage] and in controller these values get calculated and gets pushed to partial2 on click of button [ng-click="functionName()"]. This function has logic to perform the calculations. how to do this?

In the app I'm creating i've 20+ such fields in both partials, so I don't want to pass values rather get them stored in localStorage and access from there.

surbhi bakshi
  • 160
  • 1
  • 1
  • 17
  • [Usage](https://github.com/gsklee/ngStorage#usage) section from your link has the answer. – Arkantos Oct 09 '15 at 11:07
  • @Arkantos I wan't to know how I can use $scope.$storage = $localStorage with a function rather than $default. – surbhi bakshi Oct 09 '15 at 12:26
  • I get what I issue I'm having, where do I keep $log.debug("launching and saving the new value" + url); $timeout(function(){ var myWindow = $window.open("", "_self"); myWindow.document.write(response.data); }); – surbhi bakshi Oct 09 '15 at 12:54
  • Sorry I'm not able to understand what you're aiming for. With this, `$scope.$storage = $localStorage`, you're creating a reference to localStorage on your current scope and any update can be done directly through the cached reference. What exactly is the issue that you're facing ? – Arkantos Oct 09 '15 at 13:05
  • data entered in partial1 should reflect on partial2 on button click. Which isn't happening. – surbhi bakshi Oct 09 '15 at 14:12
  • If you just want to pass the data from page1 to page2, you can do that with a service itself, any specific reason for choosing LocalStorage ? – Arkantos Oct 09 '15 at 14:16
  • this is what my app does, I've 3 partial, in partial 1 all the details are entered manually, parial 2 has details that are hardcoded, partial3 has details which gets generated from permutation combination of details on partial1 and 2, they're editable and at last, everything in that app is saved using mongoDB and a file of .sh format is generated. So, I thought localStorage would be easier to work with as data gets stored in browser. No particular reason other than that. – surbhi bakshi Oct 10 '15 at 21:38

1 Answers1

15

Assuming that you have ng-model attributes for all the fields that you're trying to capture in partial1 like below.

Partial-1

<input type='text' ng-model='pageData.field1' />
<input type='text' ng-model='pageData.field2' />
<input type='text' ng-model='pageData.field3' />

app.js

    var myApp = angular.module('app', ['ngStorage']);

    myApp.controller('Ctrl1', function($scope, $localStorage){

        $scope.pageData = {};

        $scope.handleClick = function(){
           $localStorage.prevPageData = $scope.pageData; 
        };

    });

    myApp.controller('Ctrl2', function($scope, $localStorage){

        $scope.pageData = $localStorage.prevPageData;

    });

Partial-2

<p>{{pageData.field1}}</p> 
<p>{{pageData.field2}}</p> 
<p>{{pageData.field3}}</p> 

Another Approach :

But if you just want to send data from one controller in page1 to another controller in page2, you can do that with a service as well.

myApp.service('dataService',function(){

   var cache;

   this.saveData = function(data){
      cache = data;
   };

   this.retrieveData = function(){
      return cache;
   };

});

Inject this service in your first controller to save the page data and inject it again in your second controller to retrieve the saved page data.

myApp.controller('Ctrl1', function($scope, dataService){

   dataService.saveData($scope.pageData);

});

myApp.controller('Ctrl2', function($scope, dataService){

   $scope.pageData = dataService.retrieveData();

});
Arkantos
  • 6,530
  • 2
  • 16
  • 36
  • Hi, Thanks :) this absolutely solved my issue. Had to make few changes here and their, and it was good to go. Thanks a ton. :) – surbhi bakshi Oct 14 '15 at 10:54
  • I'm getting another error. I had a reset button which would clear the localStorage, but after clicking on it, my complete code stopped working. Everywhere its showing as {{$storage.pageA.XYZ}}. What could possible be the issue? [all the other angular code stopped working right after click on reset < button ng-click= "$storage.$reset() "> Reset code.] – surbhi bakshi Oct 15 '15 at 12:46
  • `$storage.$reset()` to work properly, you should have reference to $localStorage as $storage on your controller scope. Is that available ? Also do you see any errors in Dev Tools (F12) Console tab ? – Arkantos Oct 15 '15 at 14:22
  • My ng-model is $storage.pageA.X. How to work around with this? How to set default values, delete and do everything else? Refer https://github.com/gsklee/ngStorage. As I tried, didn't work for me. – surbhi bakshi Oct 16 '15 at 19:58
  • Can you only use the ngstorage in controllers? What if I have a service module that handles specific data that needs to be stored in locally, I don't think using controllers for all of this would be wise. – Philll_t Jul 17 '16 at 02:08
  • Once you list ngStorage as a dependency in your app, you should be able to inject `$localStorage` just like any other service say `$http` in your own service where you need to persist data locally – Arkantos Jul 19 '16 at 17:43