Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ajax Angular$http在每次我离开页面然后返回页面时都会触发_Ajax_Angularjs_Http_Caching - Fatal编程技术网

Ajax Angular$http在每次我离开页面然后返回页面时都会触发

Ajax Angular$http在每次我离开页面然后返回页面时都会触发,ajax,angularjs,http,caching,Ajax,Angularjs,Http,Caching,我有一个Ionic应用程序正在向服务器发出$http请求。我列出了许多文章,用户可以选择进入一篇文章。我的问题是,我注意到当我进入列出文章的页面时,它会调用以检索该文章列表。如果我离开那一页,然后再回到那一页,它会再次打那个电话。是否有一种方法可以缓存该数据,使其仅在“pull to refresh”实例中或设置计时器以进行调用时才对服务器进行调用 我的服务: .factory('Articles', function ($http) { var articles = []; stora

我有一个Ionic应用程序正在向服务器发出$http请求。我列出了许多文章,用户可以选择进入一篇文章。我的问题是,我注意到当我进入列出文章的页面时,它会调用以检索该文章列表。如果我离开那一页,然后再回到那一页,它会再次打那个电话。是否有一种方法可以缓存该数据,使其仅在“pull to refresh”实例中或设置计时器以进行调用时才对服务器进行调用

我的服务:

.factory('Articles', function ($http) {
    var articles = [];
storageKey = "articles";

    function _getCache() {
        var cache = localStorage.getItem(storageKey );
        if (cache)
            articles = angular.fromJson(cache);
    }
    return {
        all: function () {
            return $http.get("http://jsonp.afeld.me/?url=http://examplesite.com/page.html?format=json").then(function (response) {
                articles = response.data.items;
                console.log(response.data.items);
                return articles;
            });
        },
        get: function (articleId) {
            if (!articles.length) 
            _getCache();
            for (var i = 0; i < articles.length; i++) {
                if (articles[i].id === parseInt(articleId)) {
                    return articles[i];
                }
            }
            return null;
        }
    }
});

您已经在缓存数据,因此只需将
all
更改为:

all: function () {
    var cache = localStorage.getItem(storageKey);
    // If cache, return a promise wich resolves with the cache
    if (cache) {
        var deferred = $q.defer();
        deferred.resolve(angular.fromJson(cache));
        return deferred.promise;
    } else {
        // if no cache, do a http call to get the data
        return $http.get("http://jsonp.afeld.me/?url=http://examplesite.com/page.html?format=json").then(function (response) {
            articles = response.data.items;
            // store in cache
            localStorage.setItem(storageKey, articles);
            console.log(response.data.items);
        });
},

单击按钮时是否要调用后端?不只是在视图之间导航。比如,如果我有一个about页面和文章页面切换到about,那么返回到article会导致angular发出另一个服务器请求;然后,您必须找到如何操作此缓存。我记得像$cache factory之类的东西。还有一个内置缓存,你可以在调用配置中直接使用$http设置“cache:true”。是的,但是他需要本地存储缓存,正如你所看到的,在代码的其他地方,所以在本地存储中缓存可以在其他地方使用它。我得到错误$q未定义?我将它添加到我的函数中,然后得到错误:TypeError:cannotread undefinedOk的属性'defer',通过将$q添加到我的工厂函数中修复了它。再次感谢你的帮助@devqon只是一个简单的问题。这是否允许页面在服务器上更新其他文章时添加这些文章?
all: function () {
    var cache = localStorage.getItem(storageKey);
    // If cache, return a promise wich resolves with the cache
    if (cache) {
        var deferred = $q.defer();
        deferred.resolve(angular.fromJson(cache));
        return deferred.promise;
    } else {
        // if no cache, do a http call to get the data
        return $http.get("http://jsonp.afeld.me/?url=http://examplesite.com/page.html?format=json").then(function (response) {
            articles = response.data.items;
            // store in cache
            localStorage.setItem(storageKey, articles);
            console.log(response.data.items);
        });
},