简体   繁体   English

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

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

Just for example: 例如:

I have directive inside ngView with this structure: 我在ngView使用以下结构的指令:

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

When I change the route and return it back, the directive link option is executed again, the scope is empty. 当我更改路线并将其返回时,再次执行指令link选项,范围为空。 It need to wait some time for data-response from the remote server and then showing the data. 它需要等待一些时间来从远程服务器进行数据响应,然后再显示数据。 I trying to avoid the layout stretching. 我试图避免布局拉伸。

I am new to angular, I was read the documentation. 我是新手,我已阅读文档。

My question is: In this case, where the data is good to be saved? 我的问题是:在这种情况下,保存数据的位置好吗? Do I need to make a controller? 我需要做一个控制器吗? Or I can just cache it? 或者我可以缓存它?

Note: This directive repeating an array (remote data) and making "features" HTML layout. 注意:该指令重复一个数组(远程数据)并进行“功能” HTML布局。 This directive will be used in another routes with another behaviors. 该指令将在具有其他行为的其他路由中使用。

I do not need code explanations, I can read docs. 不需要代码说明,我可以阅读文档。 I accept terminology. 我接受术语。

You could store the data in a service, basically to act as a cache as you mentioned. 您可以将数据存储在服务中,基本上就像您提到的那样充当缓存。 If you need to have that data available on page reloads (hard refresh, your app is reloaded again) you can store it in a cookie/local storage. 如果您需要重新加载页面上的数据(硬刷新,再次重新加载您的应用程序),则可以将其存储在Cookie /本地存储中。

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

   }
 }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM