2017-08-30 99 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 
} 
相关问题