简体   繁体   English

如何将第三方库注入angular.js服务

[英]How to inject a third party library to an angular.js service

I am having trouble while trying to load the angular-cache library to my service. 尝试将angular-cache库加载到我的服务时遇到了麻烦。 I used bower to add angular-cache to my project and it is successfully added. 我使用bower向我的项目中添加了角度缓存,并成功添加了它。 When I debug the service code on Chrome I see in the "Networks" tab that angular-cache is loaded: 当我在Chrome上调试服务代码时,在“网络”标签中看到加载了角度缓存:

Name: angular-cache.js
Method: GET
Status: 200
Type: script
Initiator: require.js:1901
Size: 6,5 kb
Time: 16 ms

There is a config.js file that I load all my libraries. 我有一个config.js文件,它加载了所有库。 This line is for angular-cache: 这行是针对角度缓存的:

'angular-cache': '../bower_components/angular-cache/dist/angular-cache',

and this is the line inside shim: 这是垫片内的线:

'angular-cache': ['angular'],

And this is the service: 这是服务:

 define(
    [ 'angular', 'services-module'],
    function(angular, services) {
        services.factory(
          'MyService',
           [
            "$location",
            "$interval",
            "MyOtherService",
            "CacheFactory",
            function($location, $interval, otherService, cacheFactory) {

               var service = {
                    myCachingFunction : function(parameters){

                    }, 
                    getCache : function(cacheId) {

                    }
                }
                return service;
            } ]);
});

This is the error I get: 这是我得到的错误:

Error: [$injector:unpr] Unknown provider: CacheFactoryProvider

This is the github page of the angular-cache. 是angular-cache的github页面。 What am I missing? 我想念什么?

Have you tried this, I guess that this is your service.js: 您是否尝试过,我想这是您的service.js:

define(
['angular', 'services-module'],
function(angular, services) {
    services.factory(
      'MyService',
       [
        "$location",
        "$interval",
        "MyOtherService",
        "CacheFactory",
        function($location, $interval, otherService, cacheFactory) {

In your app.js: 在您的app.js中:

define(
['angular', 'angular-cache'],
function(angular) {
    var myApp = angular.module('myApp', [ 'ngResource', 'app.controllers', 'app.directives', 'app.services', 'app.filters', 'app.routes', 'app.interceptors', 'angular-cache' ]);

In config.js: 在config.js中:

    shim: {
      'angular-cache': {
         deps: ['angular']
       }
    }

I'm guessing that you are using Require with Angular. 我猜您正在使用Require with Angular。

You want to include is as a module like this: 您要包含的是这样的模块:

define(
    [ 'angular', 'services-module', 'angular-cache'],
    function(angular, services, cache) {
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM