angularjs - Ionic 本地存储和路由

标签 angularjs local-storage ionic-framework

我正在制作一个 Ionic 应用程序,并且正在利用 $http 服务从服务器提取文章。我的应用程序有一个文章列表,可以选择进入一篇文章。我的工厂是这样的:

.factory('Articles', function ($http) {
    var articles = [];
    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) {
            for (var i = 0; i < articles.length; i++) {
                if (articles[i].id === parseInt(articleId)) {
                    return articles[i];
                }
            }
            return null;
        }
    }
});

我正在使用我的 Controller 在无法获取文章的情况下显示此内容:

.controller('ThisCtrl', function ($scope, $stateParams, Articles) {
    $scope.articles = [];
    Articles.all().then(function(data){
        $scope.articles = data;
        window.localStorage.setItem("articles", JSON.stringify(data));
    }, 

    function(err) {
       if(window.localStorage.getItem("articles") !== undefined) {
          $scope.articles = JSON.parse(window.localStorage.getItem("articles"));
        }
    }

    );
})

效果很好,但我在使用单个 Controller 从 localStorage 获取一篇文章时遇到了问题:

.controller('GautengInnerCtrl', function ($scope, $stateParams, Articles) {
  $scope.articles = JSON.parse(window.localStorage.getItem("articles"));
  $scope.article = Articles.get($stateParams.articleId);
})

最佳答案

您应该检查您的服务本地存储中是否有任何文章,如果有则检索它们:

.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);
                localStorage.setItem(storageKey, 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;
        }
    }
});

关于angularjs - Ionic 本地存储和路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30750436/

相关文章:

javascript - 如何存储 Google Chrome 扩展程序的偏好设置?

css - ionic : how to center text in a card body and align

javascript - ionic 触发点击事件不起作用

javascript - 如何动态地将 DOM 元素(带有 Controller )添加到 AngularJS 范围中

javascript - 即使没有 DOM 断点,Chrome 调试器也会在一个函数处停止

javascript - localStorage - 检索复选框值

javascript - Backbonejs fullcalendar v2 从本地存储加载事件

javascript - 如何在模拟器上运行 ionic 应用程序?

javascript - AngularJs:在 Controller 内格式化当前日期

javascript - angularjs 表单重置错误