是CacheFactory在angularjs单身? [英] Is CacheFactory in angularjs a singleton?

查看:156
本文介绍了是CacheFactory在angularjs单身?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建用CacheFactory的服务。我期待这是一个单例。我注入到我的控制器,它控制器的范围内正常工作。但是,一旦我去到不同的页面,不同的范围,我似乎并没有在我存储在同一个控制器在不同的范围内缓存的值。如果不是CacheFactory的行为是单身,我有那些相同的缓存的对象我到处注入CacheService?

这是我的服务为例:

  angular.module('为MyService,[])。工厂('CacheService',函数($ cacheFactory){
        返回$ cacheFactory('cacheService',{
            容量:3 //可选 - 打开缓存到缓存LRU
        })
    });

然后在我的控制器:

 函数myController的($范围,CacheService){
   变种结果= CacheService.get('储值');
   如果(!结果){
       CacheService.put('储值',结果);
      警报('结果不存储');
   }
   其他
      警报('结果保存');
}


解决方案

$ cacheFactory 其实不是你想使用的服务 - 这是一个工厂,用来创建的你想要的单服务使用。目测您的code,它看起来像它应该工作。但我继续创建了一个演示,以证明它。这里的服务:

.factory('CacheService',函数($ cacheFactory){
   VAR缓存= $ cacheFactory('cacheService',{
     容量:3 //可选 - 打开缓存到缓存LRU
   });   返回高速缓存;
});

在这个例子中, CacheService 的一个与创建一个本地缓存的单 $ cacheFactory ,这是我们从服务返回。我们可以注入到这一点,我们希望任何控制器,它总是会返回相同的值。

下面是一个工作Plunker:<一href=\"http://plnkr.co/edit/loKWGms1lMCnmiWa1QA7?p=$p$pview\">http://plnkr.co/edit/loKWGms1lMCnmiWa1QA7?p=$p$pview


如果由于某种原因,您的文章不包含什么打破了你的code,请随时更新我Plunker打破它,我们可以从那里。

I've created a service using the CacheFactory. I was expecting it to be a singleton. I inject it into my controller and it works fine within the scope of the controller. But once I go to a different page with a different scope, I don't seem to have the values in the cache that I stored in the same controller in a different scope. Shouldn't the behavior of the CacheFactory be a singleton where I have those same cached objects everywhere I inject the CacheService?

This is my service as an example:

angular.module('MyService', []).factory('CacheService', function($cacheFactory) {
        return $cacheFactory('cacheService', {
            capacity: 3 // optional - turns the cache into LRU cache
        })
    });

Then in my controller:

function MyController($scope, CacheService) {
   var results= CacheService.get('storedvalue');
   if(!results){
       CacheService.put('storedvalue', results);
      alert('results not stored');
   }
   else
      alert('results stored');
}

解决方案

$cacheFactory is actually not the service you want to use - it's a factory that you use to create the singleton service you want to use. Eyeballing your code, it seems like it should work. But I went ahead and created a demo to prove that it does. Here's the service:

.factory('CacheService', function($cacheFactory) {
   var cache = $cacheFactory('cacheService', {
     capacity: 3 // optional - turns the cache into LRU cache
   });

   return cache;
});

In this example, CacheService is the singleton that has a local cache created with $cacheFactory, which is what we return from the service. We can inject this into any controller we want and it will always return the same values.

Here's a working Plunker: http://plnkr.co/edit/loKWGms1lMCnmiWa1QA7?p=preview


If for some reason your post doesn't contain what broke your code, please feel free to update my Plunker to break it and we can go from there.

这篇关于是CacheFactory在angularjs单身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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