0

I have an issue. I am setting the value in route page using factory method to call those value in all controller using Angular.js but when I am refreshing any controller page the undefined value is coming. I am providing my code below.

route.js:

var app=angular.module('ikomplianzNABH',['ui.router','angular.chosen']);
app.run(function($rootScope, $state) {
      $rootScope.$state = $state;
    });
app.config(function($stateProvider, $urlRouterProvider,$locationProvider) {
    $urlRouterProvider.otherwise('/');
    $stateProvider
    .state('/', { /*....This state defines All type of user login...*/
        url: '/',
        templateUrl: 'View/NABH.html',
        controller: 'NABHParentController'
    })
    .state('initiateaudit', { /*....This state defines All type of user login...*/
        url: '/initiateaudit',
        templateUrl: 'View/auditDetail.html',
        controller: 'auditdetailController'
    })
    $locationProvider.html5Mode({
      enabled: true,
      requireBase: true
    });
});
app.factory('getBothIdToAuditDetailPage',function($timeout, $window){
    var value ={};
    return{
        setClauseValue:function(cid){
            value.setClauseId=cid;
        },
        getClauseValue:function(){
            return value.setClauseId;
        }
    }
})

firstController.js:

var app=angular.module('ikomplianzNABH');
app.controller('firstController',function($scope,$http,$state,$window,getBothIdToAuditDetailPage){
    getBothIdToAuditDetailPage.setClauseValue(1234);
})

In this controller I am setting the value to controller.

secondController.js:

var app=angular.module('ikomplianzNABH');
app.controller('secondController',function($scope,$http,$state,$window,getBothIdToAuditDetailPage){
    console.log('both id'getBothIdToAuditDetailPage.getClauseValue());

})

Here I am fetching that value set in firstController.js . Here I can get the value or first time but while I am refreshing the controller/page I am getting the undefined value. Here I need that value while this conroller will execute.

halfer
  • 19,824
  • 17
  • 99
  • 186
satya
  • 3,508
  • 11
  • 50
  • 130
  • _I am refreshing the controller/page_, How are you refreshing? `F5`/Route reload??? – Satpal Oct 30 '17 at 06:19
  • I am doing `F5` or rereshing using the browser icon. – satya Oct 30 '17 at 06:21
  • Then data will lose as Angular Application will reinitialize, You can use [`sessionStorage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage) – Satpal Oct 30 '17 at 06:24
  • But I need to retain that value. Suppose someone set value already first time getting the value but while wrongly refresing the page the undefined value is coming. – satya Oct 30 '17 at 06:25
  • Like I said try with sessionStorage i.e. to set use `sessionStorage.setItem('clauseId', 'cid');` and `return sessionStorage.getItem('clauseId')` – Satpal Oct 30 '17 at 06:27
  • Ok, Will it be over write every call of that function ? – satya Oct 30 '17 at 06:27

1 Answers1

0

For persisting anything after page reload ,the best option is to use local storage for it and on logout you can clear the local storage.

On page reload check if the value is present in local storage or else fetch from network request.

https://stackoverflow.com/a/26118991/4861453

Akash Pal
  • 126
  • 1
  • 12