尝试实现Angular-Cache(Ionic)时在我的控制台中出错

时间:2015-11-15 17:23:42

标签: angularjs ionic-framework

我正在研究离子项目,我正在尝试使用Angular-cache实现缓存。实现了角度缓存,现在当我运行它时,我的控制器出现错误,但我的控制器代码中没有任何错误。

enter image description here

我app.js代码的一部分:

-keep class javax.mail.** {*;}
-keep class javax.activation.** {*;}
-keep class com.sun.mail.dsn.** {*;}
-keep class com.sun.mail.handlers.** {*;}
-keep class com.sun.mail.smtp.** {*;}
-keep class com.sun.mail.util.** {*;}

我的service.js代码:

angular.module('Chemicar', ['ionic','ChemicarProduct.controllers', 'ChemicarProduct.services', 'angular-cache'])

.run(function($ionicPlatform, CacheFactory) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }

    CacheFactory("productsCache", { storageMode: "localStorage", maxAge: 5000, deletionOnExpire: "aggressive"});

  });
})

我的controller.js代码:

angular.module('ChemicarProduct.services', [])

.service('ChemicarProductService', function($http, $q, CacheFactory){

    var url1 = "XXXXXXXXX";    
    var productsCache = CacheFactory.get('productsCache');

    var self = {
        'products' : [],
        'loadProducts' : function(){    
            var d = $q.defer(),
                cacheKey = "products",
                productsData = productsCache.get(cacheKey);

            if(productsData){
                console.log("Found data inside cache" , productsCache);
                d.resolve(productsCache);
            }
            else{
                $http.get(url1)
                .success(function(data){
                    self.products = data;
                    console.log("Received data via http " , self.products);
                    productsCache.put(cacheKey, self.products);
                    d.resolve(data);
                })
                .error(function(msg){
                    console.log("Error while making http call " , msg);
                    d.reject("The promise was not fulfilled", data);
                });
                return d.promise;
            }     
        }
    };
    return self;
});

1 个答案:

答案 0 :(得分:1)

在服务中,只有在return d.promise;返回if(productsData){}时才会致电false

这就是为什么ChemicarProductService.loadProducts()不返回promise又名

的原因
  

无法读取未定义的属性'然后'

更改为:

.service('ChemicarProductService', function($http, $q, CacheFactory){

    var url1 = "XXXXXXXXX";    
    var productsCache = CacheFactory.get('productsCache');

    var self = {
        'products' : [],
        'loadProducts' : function(){    
            var d = $q.defer(),
                cacheKey = "products",
                productsData = productsCache.get(cacheKey);

            if(productsData){
                console.log("Found data inside cache" , productsCache);
                d.resolve(productsCache);
            }
            else{
                $http.get(url1)
                .success(function(data){
                    self.products = data;
                    console.log("Received data via http " , self.products);
                    productsCache.put(cacheKey, self.products);
                    d.resolve(data);
                })
                .error(function(msg){
                    console.log("Error while making http call " , msg);
                    d.reject("The promise was not fulfilled", data);
                });
                //return d.promise;
            }

           return d.promise;      // <---- fix
        }
    };
    return self;
});