Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$cacheFactory #18

Open
Wscats opened this issue Aug 23, 2016 · 0 comments
Open

$cacheFactory #18

Wscats opened this issue Aug 23, 2016 · 0 comments

Comments

@Wscats
Copy link
Owner

Wscats commented Aug 23, 2016

$cacheFactory可以用来进行数据在controller之间传递

var cache = $cacheFactory('wsscat');
用**$cacheFactory**方法设置一个缓存,并赋给一个对象,该对象拥有下面的这些方法

  • put(key,value);
  • 在缓存对象中插入一个键值对(key,value)
  • get(key);
  • 在缓存对象中通过指定key获取对应的值
  • romove(key);
  • 在缓存对象中通过指定key删除对应的值
  • removeAll();
  • 删除缓存对象中所有的键值对
  • destroy();
  • 销毁这个缓存对象
  • info();
  • 获取缓存对象信息(id,size)

用**$cacheFactory**在控制器之间交换数据要注意的是,如果刷新页面,数据就不复存在,所以在做单页面应用时候,如果想在不同视图不同控制器之间交换数据,这种方法慎用

其实**$cacheFactory**在控制器交换数据等同于自定义一个服务,然后在自定义服务中交换数据

app.controller('pageMain1Ctrl', function($scope, $cacheFactory) {
            var cache = $cacheFactory('wsscat');
            cache.put('name', 'wsscat');
            cache.put('age', 0);
            var info = cache.info();
            console.log(info);
        })

app.controller('pageMain2Ctrl', function($scope, $state, $cacheFactory) {
            var cache = $cacheFactory.get('wsscat');
            var name = cache.get('name');
            console.log(name);
        })

这里写图片描述

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant