Ionic:导航到另一个视图时保留$ scope

Sir*_*kon 3 scope angularjs angularjs-scope ionic-framework

我正在使用Ionic Framework(Angular + Cordova)开发一个应用程序.

该应用程序有一个新闻部分,其中包含从JSON中的服务器加载的新闻列表,然后我点击一个新的打开Single New的视图,但是当返回到新闻列表时,$ scope已被清除,必须再次获取来自服务器的新闻列表.

这是通常的行为还是我做错了什么?

我怎么能阻止这种行为?

谢谢!

red*_*d-X 7

您应该将这种数据保存在单独的服务中,具体如下:

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;
    })
}]);
Run Code Online (Sandbox Code Playgroud)