我有过
<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:
angular.module("myApp", ["ngCookies"]).
config(function($cookies) {
console.log($cookies.myCookie);
});但它似乎没有找到$cookies:
Unknown provider: $cookies from myApp发布于 2013-03-12 19:18:12
我不确定你的功能用例是什么,但是你不能在配置块中注入服务($cookies是一个服务)。只有常量和提供程序才能注入到配置块中。
您可以将服务注入run块,但我不知道这是否对您有帮助,因为我不确定您要用这些cookie做什么。
顺便说一句,您似乎混淆了主angular文件和ngCookies模块的不同版本。这与你的问题没有直接联系,但这是相当奇怪的。
发布于 2014-11-27 18:10:38
您可以手动注入:
myApp.config(function() {
var $cookies;
angular.injector(['ngCookies']).invoke(['$cookies', function(_$cookies_) {
$cookies = _$cookies_;
}]);
// here you can use $cookies as usual
});您可能想知道,为什么我们必须为injector()调用和.invoke()调用指定ngCookies?
ngCookies是模块的名称(您的项目中需要有angular-cookies.js才能使用此模块)。当您通过调用injector()创建注入器时,您需要指定应该使用哪些模块,以便可以使用来自这些模块的服务。
invoke()调用一个函数,但是让我们指定应该将哪些服务(由各个模块提供)传递给该函数(在我们的示例中,由ngCookies模块提供的服务被命名为$cookies)
这个解决方案有点麻烦,因为你手动创建了一个新的注入器,而不是你的angular应用程序自动创建和使用的注入器,但它应该是安全的,因为ngCookie似乎只使用了angular的功能,这些功能不会保持自己的状态,只是浏览器功能的薄薄包装。
发布于 2015-06-09 22:17:22
我遇到了同样的问题。angular版本和angular cookies版本不匹配。我所做的是将bower.json更新到这两个版本的工作版本。
"angular": ">=1.2.*",
"angular-cookies": ">=1.2.*"然后我运行:
bower install我得到了这个问题:
Unable to find a suitable version for angular, please choose one:
1) angular#1.3.13 which resolved to 1.3.13 and is required by angular-cookies#1.3.13
2) angular#>=1.2.x <=1.4.x which resolved to 1.3.16 and is required by oclazyload#1.0.1
3) angular#1.4.0 which resolved to 1.4.0 and is required by angular-cookies#1.4.0
4) angular#>=1.2.* which resolved to 1.4.0 and is required by hashve
5) angular#>=1 which resolved to 1.4.0 and is required by angular-bootstrap#0.11.2
6) angular#>=1.2.26 <=1.5 which resolved to 1.4.0 and is required by angular-translate#2.7.2
7) angular#~1.x which resolved to 1.4.0 and is required by restangular#1.5.1
8) angular#^1.2.6 which resolved to 1.4.0 and is required by angular-socket-io#0.6.1
9) angular#>= 1.0.8 which resolved to 1.4.0 and is required by angular-ui-router#0.2.15
10) angular#>=1.2.0 <1.5.0 which resolved to 1.4.0 and is required by angular-moment#0.10.1Prefix the choice with ! to persist it to bower.json然后我选择了3。
而且它起作用了。
https://stackoverflow.com/questions/15358029
复制相似问题