即使AngularJS模块加载失败,如何继续运行

时间:2013-11-15 08:32:14

标签: angularjs

示例:

angular.module("app", ['someServices'])

如果someServices不存在,将收到错误消息:

Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:modulerr] Failed to instantiate module pascalprecht.translate32 due to:
Error: [$injector:nomod] Module 'someServices' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

但是现在,我想让它继续运行,这意味着如果加载失败则忽略它。

这有配置吗?

1 个答案:

答案 0 :(得分:1)

我仍然不明白为什么你需要这样做。但是根据AngularJS的不同,它无法绕过它。如果你想做这样的事情,你总是可以先检查模块是否存在,如果它不存在则不包含在依赖项中。像这样:

var dependencies = [];
if (angular.module('someServices')) dependencies.push( 'someServices' );
var myApp = angular.module("app", dependencies);