离子:导航到另一个视图时保留 $scope [英] Ionic: Preserve $scope when navigate to another view
本文介绍了离子:导航到另一个视图时保留 $scope的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 Ionic Framework (Angular+Cordova) 开发一个应用.
I'm developing an app using Ionic Framework (Angular+Cordova).
该应用程序有一个新闻部分,其中包含从服务器加载的 JSON 新闻列表,然后我点击一个新的以打开单个新闻的视图,但是当返回新闻列表时,$scope 已被清除,并且必须再次从服务器获取新闻列表.
The app have a News section with a list of news loaded from a server in JSON, then I tap in a new to open the Single New's View, but when go back to the list of news, $scope has been cleared and must get again the news list from the server.
这是通常的行为还是我做错了什么?
Is this the usual behavior or am I doing something wrong?
我怎样才能防止这种行为?
How could I prevent this behavior?
谢谢!
推荐答案
您应该将此类数据保存在单独的服务中,如下所示:
You should save this kind of data in a separate service, something in the line of this:
app.service('NewsService', ['$http', function($http){
var newsPromise;
this.getNews = function(){
if(!newsPromise){
newsPromise = $http.get('news.json');
}
return newsPromise;
};
}]);
app.controller('NewsController', ['$scope','NewsService', function($scope, NewsService){
NewsService.getNews().then(function(data){
$scope.news = data.data;
})
}]);
这篇关于离子:导航到另一个视图时保留 $scope的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文