在哪里存储 ngView 中指令使用的远程数据? (AngularJS)

Where to store remote data used by directive inside ngView? (AngularJS)

举个例子:

我在 ngView 中有这样结构的指令:

.directive('features', function() {
    templateUrl: '.../',
    link: function(scope) {
        // HTTP req, getting remote data.
        // Store the remote data to the scope
    }
})

当我更改路由并 return 返回时,再次执行指令 link 选项,范围为空。需要等待一段时间远程服务器的数据响应,然后显示数据。我试图避免布局拉伸。

我是 angular 的新手,我已经阅读了文档。

我的问题是:这种情况下,数据保存在哪里比较好?我需要做一个控制器吗?或者我可以缓存它?

注意:该指令重复一个数组(远程数据)并进行 "features" HTML 布局。该指令将用于具有其他行为的其他路由。

不需要代码解释,我可以阅读文档。我接受术语。

您可以将数据存储在服务中,基本上就像您提到的那样充当缓存。如果您需要在页面重新加载时提供该数据(硬刷新,您的应用会再次重新加载),您可以将其存储在 cookie/local 存储中。

app.service("featuresService",function($http){
 var cachedFeatures=null;
 return{
   getFeatures:function(callback){
     if(!cachedFeatures){
       $http.get("...").success(function(data){
             cachedFeatures=data;
             callback(data);
       })
     }else{
             callback(cachedFeatures);
     }

   }
 }
})