48

我试图在多个页面中使用的AngularJS中实现一个控制器。它利用一些服务。其中一些是在所有页面上加载的,有些则不是。我的意思是它在不同的文件中定义,并且这些文件是独立加载的。但是,如果我不能在所有的网页加载这些服务我得到错误:AngularJS中的可选依赖关系

Error: Unknown provider: firstOtionalServiceProvider <- firstOtionalService 

所以,我需要的所有网页上加载脚本。我可以在Angular中声明依赖项作为可选项吗?例如:

myApp.controller('MyController', ['$scope', 'firstRequiredService', 'secondRequiredService', 'optional:firstOptionalService', 'optional:secondOptionalService', function($scope, firstRequiredService, secondRequiredService, firstOptionalService, secondOptionalSerivce){ 

    // No need to check, as firstRequiredService must not be null 
    firstRequiredService.alwaysDefined(); 

    // If the dependency is not resolved i want Angular to set null as argument and check 
    if (firstOptionalService) { 
     firstOptionalService.mayBeUndefinedSoCheckNull(); 
    } 

}]); 

回答

50

不,Angular目前还不支持可选的依赖关系。你最好把所有的依赖关系放到一个模块中,并把它作为一个Javascript文件加载。如果您需要另一组依赖关系 - 请考虑在另一个JS中创建另一个模块,并将所有常见的依赖关系应用于常见的JS。

但是,您所描述的行为可以使用$injector service来实现。您只需将$injector而不是将所有依赖关系注入控制器,然后手动从其中获取依赖关系,并检查它们是否存在。就是这样:

的index.html:

<!DOCTYPE html> 
<html data-ng-app="myApp"> 
    <head> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js"></script> 
    <script src="app.js"></script> 
    <script src="1.js"></script> 
    <script src="2.js"></script> 
    <title>1</title> 
    </head> 
    <body data-ng-controller="DemoController"> 
    </body> 
</html> 

app.js:

var myApp = angular.module('myApp', []); 

myApp.service('commonService', function(){ 
    this.action = function(){ 
     console.log('Common service is loaded'); 
    } 
}); 

myApp.controller('DemoController', ['$scope', '$injector', function($scope, $injector){ 
    var common; 
    var first; 
    var second; 

    try{ 
     common = $injector.get('commonService'); 
     console.log('Injector has common service!'); 
    }catch(e){ 
     console.log('Injector does not have common service!'); 
    } 
    try{ 
     first = $injector.get('firstService'); 
     console.log('Injector has first service!'); 
    }catch(e){ 
     console.log('Injector does not have first service!'); 
    } 
    try{ 
     second = $injector.get('secondService'); 
     console.log('Injector has second service!'); 
    }catch(e){ 
     console.log('Injector does not have second service!'); 
    } 

    if(common){ 
     common.action(); 
    } 
    if(first){ 
     first.action(); 
    } 
    if(second){ 
     second.action(); 
    } 
}]); 

1.js:

myApp.service('firstService', function(){ 
    this.action = function(){ 
     console.log('First service is loaded'); 
    } 
}); 

个2.js:

myApp.service('secondService', function(){ 
    this.action = function(){ 
     console.log('Second service is loaded'); 
    } 
}); 

亲身体验在this plunk!尝试玩<script>标签并观看控制台输出。

P.S.而且,@Problematic说,你可以使用从AngularJS 1.1.5开始的$injector.has()

+0

谢谢!正是我需要的。我正在使用最新的Angular,所以请使用'has'! – molaccha

56

显然不使用自动注射。但是,您可以注射进样器并检查服务:

myApp.controller('MyController', [ 
    '$scope', '$injector', 'firstRequiredService', 'secondRequiredService', 
    function ($scope, $injector, firstRequiredService, secondRequiredService) { 
     if ($injector.has('firstOptionalService')) { 
      var firstOptionalService = $injector.get('firstOptionalService'); 
     } 
    } 
]); 
+0

酷!我想他们backport'$ injector.has'到1.0.7! – madhead

+3

不错!这是从我的角度来看的正确答案 – Gabriel

+2

我喜欢@ madhead的这个答案,因为它使用'$ injector.has'而不是'try' /'catch' –

13

我可能会用@Proplematic的建议使用$注入器。但是,我可以想到另一种解决方案:将所有服务的默认值(例如null)注册到引导文件中。当加载额外的文件时,后面的定义将覆盖默认定义,从而在某种程度上创建您所期望的效果。

var app = angular.module('plunker', []); 

app.value("service1", null) 
    .value("service2", null) 
    .factory("service1", function() { return "hello"; }); 

app.controller('MainCtrl', function($scope, service1, service2) { 
    console.log(service1); // hello 
    console.log(service2); // null 
}); 

Demo link

+0

这是哈克,但工程。我使用它的地方有多条共享控制器的路由,但只有少数需要解决:注入的属性。 – httpete

+0

这是带有可选参数的项目的最佳答案。 – sean

+0

这正是我想要的!谢谢! 我需要使用与模态控制器相同的控制器,也需要使用ng控制器。而对于模态控制器,我需要额外的解析器。 –

5

尝试这样..

try { 
    angular.module('YourModule').requires.push('Optional dependency module'); 
} catch(e) { 
    console.log(e) 
} 

'需要' 是的依赖性模块的阵列。

9

这是我如何解决它:

var deps = []; 

try { 
    //Check if optionalModule is available 
    angular.module('app').requires.push('optionalModule'); 
    deps.push('optionalModule'); 
} catch(e){ 
    console.log("Warn: module optionalModule not found. Maybe it's normal"); 
} 

angular.module('app', deps).factory('stuff', function($injector) { 
    var optionalService; 

    if($injector.has('optionalService')) { 
     optionalService = $injector.get('optionalService'); 
    } else { 
     console.log('No waffles for you, dear sir'); 
    } 
});