为什么我无法注入 angular-cookies? [英] Why am I unable to inject angular-cookies?

查看:23
本文介绍了为什么我无法注入 angular-cookies?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

<body ng-app="myApp">
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular-cookies.min.js">
    </script>
</body>

一切正常加载.

然后在我的 javascript 中我尝试注入 ngCookies:

Then in my javascript I attempt to inject ngCookies:

angular.module("myApp", ["ngCookies"]).
    config(function($cookies) {
         console.log($cookies.myCookie);
    });

但是好像没有找到$cookies:

But it does not seem to find $cookies:

 Unknown provider: $cookies from myApp

推荐答案

我不确定你的功能用例是什么,但你不能注入服务($cookies 是一种服务)在配置块内.配置块中只能注入常量和提供者.

I'm not sure what is your functional use-case but you can't inject services ($cookies is a service) inside config blocks. Only constants and providers can be injected inside config blocks.

您可以将服务注入运行块,但我不知道这是否对您有帮助,因为我不确定您想用这些 cookie 做什么.

You can inject services into run blocks but I don't know if this helps you since I'm not sure what are your trying to do with those cookies.

顺便说一句:您似乎混合了主角度文件和 ngCookies 模块的版本.这与您的问题没有直接关系,但这很奇怪.

BTW: you seems to be mixing versions of the main angular file and the ngCookies module. This is not directly linked to your problem but this is rather odd.

这篇关于为什么我无法注入 angular-cookies?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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