从指令中的localStorageService访问数据

时间:2019-02-09 18:36:08

标签: angularjs dependency-injection angularjs-directive

如何从localStorageService中的directive获取数据?

对此进行了尝试:var clientItem = localStorageService.get('clientData');

错误:无法读取未定义的属性“ get”

2 个答案:

答案 0 :(得分:0)

在指令中或任何地方都是同一件事:

localStorageService.getItem('clientData');

here

答案 1 :(得分:0)

将服务注入到指令的构造函数中的指令中:

app.directive("myDirective", function (localStorageService) {
    return {
        link: postLink,
    };
    function postLink(scope,elem,attrs) {
        var clientItem = localStorageService.get('clientData');
    }
}

有关更多信息,请参见AngularJS Developer Guide - Dependency Injection