离子本地存储和路由
Ionic local storage and routing
我正在制作一个 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('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"));
}
}
);
})
效果很好,但我无法使用我的单个控制器从 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;
}
}
});
我正在制作一个 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('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"));
}
}
);
})
效果很好,但我无法使用我的单个控制器从 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;
}
}
});