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

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

我在尝试将 angular-缓存库加载到我的服务时遇到问题。我使用 bower 将 angular-cache 添加到我的项目中并成功添加。当我在 Chrome 上调试服务代码时,我在 "Networks" 选项卡中看到 angular-cache 已加载:

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

有一个 config.js 文件,我加载了我所有的库。此行用于 angular-缓存:

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

这是 shim 内的行:

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

这是服务:

 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;
            } ]);
});

这是我得到的错误:

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

This 是 angular-缓存的 github 页。我错过了什么?

您想要像这样作为一个模块包含在内:

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

你试过了吗,我猜这就是你的service.js:

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

在你的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' ]);

在config.js中:

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

我猜你正在使用 Angular 的 Require。