简体   繁体   中英

Value is vanishing after refresh the controller using factory method o angular.js

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM