Javascript 如何在angularjs中配置依赖项注入?

Javascript 如何在angularjs中配置依赖项注入?,javascript,configuration,angularjs,dependency-injection,Javascript,Configuration,Angularjs,Dependency Injection,我对angularjs应用程序的工作原理有点困惑 首先,我应该说我是一个新手angularjs用户,但我熟悉其他语言中的其他DI框架(比如PHP中的symfony、Java中的spring,还有一点Unity) 每个DI实现都需要类定义和DI配置 angular.module('notify', [], function($provide) { $provide.factory('LogNotifier', function($window) { return {

我对angularjs应用程序的工作原理有点困惑

首先,我应该说我是一个新手angularjs用户,但我熟悉其他语言中的其他DI框架(比如PHP中的symfony、Java中的spring,还有一点Unity)

每个DI实现都需要类定义和DI配置

angular.module('notify', [], function($provide) {
    $provide.factory('LogNotifier', function($window) {
        return {
            messageMe: function(message) {
                $window.console.log(message);
            }
        }

    });
    $provide.factory('AlertNotifier', function($window) {
        return {
            messageMe: function(message) {
                $window.alert(message);
            }
        }
    });
});



angular.module('myModule', ['notify'], function($provide) {
  // as notifier dependency I must specify its type.
  // is there any way how I would configure after its definition
  // which notifier implementation should be used?
  $provide.factory('DataLoader', function(AlertNotifier) {
      var loader = {
          loadAllItems: function() {
              AlertNotifier.messageMe('Loading items');
              // let's asume there is ajax call and promise object return
              return [ 
                    {name: 'Item1'},
                    {name: 'Item2'}
                ];
          }
      }
      return loader;
  });
});
配置通常包括:

  • 应如何类注入(自动按名称或类型,或手动)
  • 如果容器应该返回一个单例实例
  • 创建对象应使用哪个工厂类
  • serviceId-每个服务都可以通过serviceId检索。此serviceId表示创建实例的配置(应注入哪些服务以及应使用哪些参数)
  • 等等
这是我在angularjs中缺少的配置

有一个愚蠢的示例我希望这样的配置应该如何工作。我有两个服务,每个都做类似的事情,但实现方式不同

angular.module('notify', [], function($provide) {
    $provide.factory('LogNotifier', function($window) {
        return {
            messageMe: function(message) {
                $window.console.log(message);
            }
        }

    });
    $provide.factory('AlertNotifier', function($window) {
        return {
            messageMe: function(message) {
                $window.alert(message);
            }
        }
    });
});



angular.module('myModule', ['notify'], function($provide) {
  // as notifier dependency I must specify its type.
  // is there any way how I would configure after its definition
  // which notifier implementation should be used?
  $provide.factory('DataLoader', function(AlertNotifier) {
      var loader = {
          loadAllItems: function() {
              AlertNotifier.messageMe('Loading items');
              // let's asume there is ajax call and promise object return
              return [ 
                    {name: 'Item1'},
                    {name: 'Item2'}
                ];
          }
      }
      return loader;
  });
});

我想在不更改DataLoader服务的源代码的情况下,在LogNotifier和AlertNotifier之间切换。可能吗


谢谢你

与angularjs相处了一周后,我意识到一件非常重要的事情。所有angularjs示例实际上都包括我寻找的DI配置配置实际上是模块定义本身

我所要做的就是将模块定义与类定义分开。 我的问题已经解决了-

还有一些与DI配置分离的类定义

/**
* Controller class definition
*/
my = window.my || {};
my.controller = my.controller || {};
my.controller.ItemCtrl = function($scope, loader) {
    $scope.items = loader.loadAllItems();
};

my.controller.CachedCtrl = function($scope) {
    $scope.items = [
        { name: 'Cached ctrl value 1'},
        { name: 'Cached ctrl value 2'}
    ]
}

/**
* Model class definition
*/
my.model = my.model || {};
my.model.DataLoader = function(notifier) {
    this.notifier = notifier;
    this.loadAllItems = function() {
        this.notifier.messageMe('Loading items');
        // let's asume there is ajax call and promise object return
        return [ 
            {name: 'Item1'},
            {name: 'Item2'}
        ];
    };
};

/**
* Some debug classes definition
*/
my.debug = my.debug || {}
my.debug.LogNotifier = function($window) {
    this.$window = $window;
    this.messageMe = function(message) {
        this.$window.console.log(message);
    }
};
my.debug.AlertNotifier = function($window) {
    this.$window = $window;
    this.messageMe = function(message) {
        this.$window.alert(message);
    }
}
我想这是实现我的要求的最干净的方法。不幸的是,如果你真的想这样做,你必须写更多的代码


Frank

我想知道如何将AngularJS优雅的DI引入PHP DIs。目前,PHPDI看起来太像服务容器了。