2017-08-30 24 views
1

當前在我的_Layout.cshtml中的設置我有標準的腳本標記文件路徑到幾個像這樣的angularjs文件。js文件未找到,當我把它放在一個包中

<script src="~/Scripts/angularjs/Controller/MyController.js"></script>...(plus a few more) 

因此,我不想在_layout文件中明確地包含所有這些文件,而是想將它們捆綁在一起。所以我在BundleConfig.cs中這樣做了。

bundles.Add(new ScriptBundle("~/Scripts").Include(
"~/angularjs/Factory/authService.js", 
"~/angularjs/Controller/MyController.js")); 

一切都在建立。但是當我運行它時,瀏覽器控制檯窗口給我一個角度錯誤,說它找不到我的authService.js文件。官方錯誤如下。

Uncaught Error: [$injector:unpr] http://errors.angularjs.org/1.5.8/ $injector/unpr?p0=authServiceProvider%20%3C-%20authService

所以我的問題是爲什麼當我捆綁我的腳本時是否看不到authService文件。請記住,如果我明確地說,我的角度和網頁正常工作,沒有錯誤。 (我也混淆了我在捆綁中調用它們的順序,但仍然無法使網頁正常工作)。

任何幫助將不勝感激。

編輯:在這裏提供一點點角碼是我的服務。

(function() { 

    var authService = function ($http, $q, localStorageService) { 
    /*bunch of code*/ 
}; 

    var module = angular.module("mainApp"); 

    module.factory("authService", authService); 



}()); 

所以我做了如下的變化,但我仍然在「〜腳本/ angularjs」的Web瀏覽器與403錯誤迴應的附加誤差相處了同樣的錯誤。

(function() { 

    var module = angular.module("mainApp"); 

    module.factory("authService", authService); 

    authService.$inject("$http", "$q", "localStorageService"); 

    var authService = function ($http, $q, localStorageService) { 
/*my code*/ 
}; 
}()); 

最終解決方案: 爲了澄清,我將寄我爲了做它的工作。關鍵是@Tommaso Sebastianelli指出要傳遞[]中的module.factory行的依賴關係。非常感謝您的及時回覆。

(function() { 

    var authService = function ($http, $q, localStorageService) { 
/*my code here*/ 

}; 
    var module = angular.module("mainApp"); 

    module.factory("authService", ["$http", "$q","localStorageService", authService]); 

}()); 
+0

誤差不說這是不可能找到authservice.js文件,它說, Angular無法找到'authService'。 – phuzi

+0

我通過將我的包的名稱從「〜/ Scripts」更改爲「〜/ angular/scripts」來解決我的403錯誤,原因是前者是實際路徑。 – jtslugmaster08

回答

2

有沒有可能是你在你的角度服務不顯式依賴注入?例如:

yourapp.service('authService', function(dependancy1, dependancy2, etc){ 

}); 

這種錯誤發生在我多次最小化和捆綁模塊時。 如果是這樣的話解決類似這樣的代碼:

yourapp.service('authService', ['dependancy1', 'dependancy2', '...', 
    function (dependancy1, dependancy2, etc){ 

}]); 

最好的選擇

yourapp.service('authService', authService); 

authService.$inject = ['dependency1', 'dependency2', '...']; 

function authService(){ 
//code here 
} 
相關問題