[英]How to properly inject a dependency in Angular module in Web API?
我有一個objectFactory.js文件:
(function () {
var objectiveFactory = function ($http, $ngAnimate) {
debugger;
return {
getObjectives: function () {
return $http.get('/api/Objective/');
}
};
};
debugger;
try {
//objectiveFactory.$inject = ['$http', '$ngAnimate'];// not working
objectiveFactory.$inject = ['$http'];// perfectly works!
angular.module('app', []).factory('objectiveFactory', objectiveFactory);
}
catch (e)
{
var e1 = e;
}
}());
確實很奇怪,但是如果我添加新的依賴項ngAnimate
:
objectiveFactory.$inject = ['$http', '$ngAnimate'];// not working
然后我有一個錯誤:
angular.js:13920錯誤:[$ injector:unpr]未知提供程序:ngAnimateProvider <-ngAnimate <-ObjectiveFactory http://errors.angularjs.org/1.5.8/ $ injector / unpr?p0 = ngAnimateProvider%20%3C- %20ngAnimate%20%3C-%20objectiveFactory at angular.js:68 at angular.js:4511 at Object.getService [as get](angular.js:4664)at angular.js:4516在getService(angular.js:4664) )處的object.invoke(angular.js:4710)處的jectionArgs(angular.js:4688)處,object.invoke(angular.js:4718)處的Object.enforcedReturnValue [as $ get](angular.js:4557) .js:4517
但是'$http'
注入非常有效。
我探索了很多信息,並在我的Web API應用程序中仔細檢查了以下建議:
我已經檢查了angular'js
文件和angular-animate.js
版本,它們是相同的1.5.8。
我已經排除了縮小和捆綁的問題,因此文件的加載方式如下例所示:
<script src="@Url.Content("~/Scripts/jquery-1.10.2.js")"></script> <script src="@Url.Content("~/Scripts/bootstrap.js")"></script> <script src="@Url.Content("~/Scripts/angular.js")"></script> <script src="@Url.Content("~/Scripts/angular-route.js")"></script> <script src="@Url.Content("~/Scripts/angular-animate.js")"></script> <script src="@Url.Content("~/Scripts/objectiveFactory.js")"></script> <script src="@Url.Content("~/Scripts/objective.js")"></script>
但是,錯誤是相同的:
angular.js:13920錯誤:[$ injector:unpr]未知提供程序:
有人知道我做錯了嗎? ( '$http'
注入非常有效)
ngAnimate
是模塊名稱,您應該在主模塊中注入ngAnimate
。 angular.module('app', [])
這是您的主要模塊:
angular.module('app', [$ngAnimate']).factory('objectiveFactory', objectiveFactory);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.