Angularjs 将ngStorage设置为变量值(字符串)

Angularjs 将ngStorage设置为变量值(字符串),angularjs,parse-platform,local-storage,Angularjs,Parse Platform,Local Storage,我试图提取一个工厂,它从Parse.com检索数据 理想情况下,我希望使用此代码传入我正在查询的类(字符串变量)的名称,然后使用与变量相同的键将返回值设置为$localStorage(from) 例如: .factory('userDataFactory', function(ParseFactory, $localStorage, errorFactory, _) { var userQuery = { 'userId': Parse.User.current().id

我试图提取一个
工厂
,它从Parse.com检索数据

理想情况下,我希望使用此代码传入我正在查询的类(字符串变量)的名称,然后使用与变量相同的键将返回值设置为
$localStorage
(from)

例如:

.factory('userDataFactory', function(ParseFactory, $localStorage, errorFactory, _) {
    var userQuery = {
        'userId': Parse.User.current().id
    };
    return {

        getData: function(targetClass) {
            ParseFactory.provider(targetClass, userQuery).getAll().success(function(data) {
                var flattened = _.extend.apply(_, data.results);

                    // insert variable name here
                    $localStorage.targetClass = flattened;


                }
            }).error(function(response) {
                return errorFactory.checkError(response);
            });
        }
    }
})

您会注意到我正在从这个
工厂给其他工厂打电话。工厂
这会被认为是“坏形式”吗

这一点都没错。从服务调用其他服务在Angular中很常见(请考虑使用$http或其他Angular内置服务时)。我看到的唯一问题是,您正在使用Parse对象,而没有将其作为依赖项注入。如果这也是一个角度服务,只需像ParseFactory一样注入它。

答案是:

$localStorage[targetClass]=扁平化


使用方括号而不是

你好,德里克,你能根据我输入的变量值帮我设置本地存储吗?如果我输入$sessionStorage['mysession']工作完美,但在我的chrome开发者面板中,它显示了类似于“ngStorage mysession”的内容。你知道我如何避免使用默认名称而只使用“mysession”吗?这不是我试图说的。可能需要查看存储库中的引擎盖下。