1

My code is as shown below:

angular.module('xyz.homeController', [])
    .controller('homeController', ['homeService', '$scope', '$location', '$modal', '$rootScope', '$localstorage', '$window', 'GoogleSignin'
        function(homeService, $scope, $location, $modal, $rootScope, $localstorage, $window, GoogleSignin) {

])




angular.module('xyz.homeService', [])
    .factory('homeService', function() {
        var data = {};
        var deliveryOption = 0;
        var payCardOption = 0;
        var orderId = '';
        var restaurentID = '';
        var orderInfo = {};
        var orderItems = [];

        data.setDeliveryOption = function(info) {
            this.deliveryOption = info;
        };

        data.getDeliveryOption = function() {
            return this.deliveryOption;
        };

        data.setOrderId = function(orderId) {
            this.orderId = orderId;
        };

        data.getOrderId = function() {
            return this.orderId;
        };

        data.setRestaurentId = function(id) {
            this.restaurentID = id;
        };

        data.getRestaurentID = function() {
            return this.restaurentID;
        };

        data.setOrderInfo = function(info) {
            this.orderInfo = info;
        };

        data.getOrderInfo = function() {
            return this.orderInfo;
        };

        data.setOrderItems = function(items) {
            this.orderItems = items;
        };

        data.getOrderItems = function() {
            return this.orderItems;
        };

        data.setPayCardOption = function(payCardOption) {
            this.payCardOption = payCardOption;
        };

        data.getPayCardOption = function() {
            return this.payCardOption;
        };

        return data;
    });

Now when refresh is pressed , the route is called perfectly, I have handlede the information inside the route perfectly, but somehow I am not able to restore the state of app and as a result of that, I am not able to use homeService perfectly, how to get the reference of homeService perfectly, so that I can use it?

Mrugesh
  • 4,381
  • 8
  • 42
  • 84

1 Answers1

1

The term you're looking for is singleton.

All services in AngularJs are singletons but you are using factory which is initialized by controller.

This will solve your problem if you're swithcing from one controller to the other.

However if you're refreshing the page, the application will reboot. There's no way around it than using localstorage, sessionstorage, cookies...etc.

These are available through the $window service.

gyc
  • 4,300
  • 5
  • 32
  • 54