2012-05-01 58 views
2

Angular中的所有DI文档似乎都基于这样的想法,即在代码生成时以及在运行自动化测试时希望系统实现服务,几个模拟实现。不,我有几个依赖项的工作实现,我想选择一个(通常基于URL)。我怎么做?在生产中的Angularjs中依赖项之间进行选择

回答

3
  1. 将你的依赖关系放到单独的模块中。富勒例如serviceA.impl1和serviceA.impl2
  2. 创建应用程序级别的模块,但添加依赖基于URL
 
angular.module('impl1', []).... 
angular.module('impl2', []).... 

var deps = []; 
if (location.match(...) { 
    deps.push('impl1') 
} else { 
    deps.push('impl2') 
} 

angular.module('myApp', deps); 

然后在您的index.html做

 
    <html ng-app="myApp"> 
+0

谢谢,MISKO。一旦我写了第一个模块(大约10分钟前),该解决方案就不可避免了。 – Malvolio