2013-11-15 54 views
1

例如:如何保持運行,即使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. 

但現在,我想它的持續運作,這意味着如果加載失敗,則忽略它。

這是否有一些配置?

+0

爲什麼你注入的東西不存在?我沒有明白做那樣的事情。 – tschiela

+0

模塊從其他人的配置中加載。也許這個人犯了一個錯誤:) 當發生這種情況時,我們只是想提醒他,不要讓應用程序停止。 –

回答

1

我仍然不明白爲什麼你需要這樣做。但是,僅憑AngularJS就沒有辦法。如果你想做這樣的事情,你可以先檢查模塊的存在,如果它不存在,就不要將它包含在依賴關係中。像這樣的:

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

我有一個很好的例子。角度模塊的依賴性正成爲特定瀏覽器中的錯誤的受害者。這個模塊不是核心功能。我想盡量減少這個模塊無法加載的影響,所以應用程序的其餘部分可以這樣做。 – Bon