按下刷新时,angular无法获得服务

时间:2018-01-03 13:29:28

标签: angularjs

我的代码如下所示:

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;
    });

现在当按下刷新时,路线被完美地调用,我已完全处理路线内的信息,但不知怎的,我无法恢复应用程序的状态,因此,我无法使用完全homeService,如何完美地获取homeService的引用,以便我可以使用它?

1 个答案:

答案 0 :(得分:1)

您正在寻找的术语是 singleton

All services in AngularJs are singletons但您使用的是由控制器初始化的 factory

如果您从一个控制器切换到另一个控制器,这将解决您的问题。

但是,如果您要刷新页面,应用程序将重新启动。 除了使用localstorage,sessionstorage,cookies等等之外别无选择。

这些可通过$window服务获得。