离子型:当导航到其他视图preserve $范围 [英] Ionic: Preserve $scope when navigate to another view

查看:294
本文介绍了离子型:当导航到其他视图preserve $范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用离子型框架的应用程序(角+科尔多瓦)。

I'm developing an app using Ionic Framework (Angular+Cordova).

该应用程序具有与从JSON服务器加载新闻列表的新闻部分,然后我在一个新开的单一的新的观点挖掘,但是当回到新闻列表,$范围已清除,必须从服务器再次获取新闻列表。

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?

我怎么能prevent这种行为?

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;
    })
}]);

这篇关于离子型:当导航到其他视图preserve $范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆