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

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

问题描述

 <机身NG-应用=对myApp>
    &LT;脚本的src =// ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js\"></script>
    &LT;脚本的src =// ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular-cookies.min.js\">
    &LT; / SCRIPT&GT;
&LT; /身体GT;

一切正确​​加载。

然后在我的javascript我试图注入ngCookies:

  angular.module(对myApp,[ngCookies])。
    配置(函数($饼干){
         的console.log($ cookies.myCookie);
    });

但它似乎没有找到$饼干:

 未知提供商:从对myApp $饼干


解决方案

我不知道你是什么功能的用例,但你不能注入服务( $饼干是一个服务)配置块中。只有常量和供应商可以配置块中被注入。

您可以注入服务整合到运行块,但我不知道这是否可以帮助你,因为我不知道什么是你想用这些Cookie做。

BTW:你似乎是混合角主文件和 ngCookies 模块的版本。这不是直接链接到你的问题,但是这是相当奇怪的。

I have

<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>

Everything loads correctly.

Then in my javascript I attempt to inject ngCookies:

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

But it does not seem to find $cookies:

 Unknown provider: $cookies from myApp

解决方案

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.

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.

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.

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

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