2016-05-24 33 views
0

我有一個SPA應用程序可以正常使用至今。它是使用AngularJS(+其他庫)在JavaScript中開發的。現在縮小JavaScript使用AngularJS - 扶養注射錯誤

,我希望再縮小腳本和我測試的YUICompressor都和谷歌的編譯器。

當我部署腳本的精縮版和測試它,我得到一個錯誤。

之前縮小文件的JavaScript文件是:

var MyApp_Sim_Web = angular.module('MyApp_Sim_Web', ['ngRoute' , 'ngSanitize']) ; 


//----------------------------------------------------------------------------------------------------------------------------------------------------------------- 
//----------------------------------------------------------------------- $routeProvider ------------------------------------------------------------------------ 
//----------------------------------------------------------------------------------------------------------------------------------------------------------------- 


    MyApp_Sim_Web.config(function($routeProvider) { 

     $routeProvider 

      .when ('/Login', { 
       templateUrl: 'Pages/Login.html' , 
       controller: 'LoginController' 
      }) 

      .when ('/', { 
       templateUrl: 'Pages/Login.html' , 
       controller: 'LoginController' 
      }) 
      .when ('/User_Main', { 
       templateUrl: 'Pages/User_Main.html' , 
       controller: 'UserController' 
      }) 
      .otherwise({ redirectTo: '/' }); 

    }); 

//----------------------------------------------------------------------------------------------------------------------------------------------------------------- 
//----------------------------------------------------------------------- $IndexController ---------------------------------------------------------------------- 
//----------------------------------------------------------------------------------------------------------------------------------------------------------------- 

    MyApp_Sim_Web.filter('Make_Timestamp_Readable', function() { 
     return function(input) { 
      var date = new String(input), 
       year = date[ 0] + date[ 1] + 
          date[ 2] + date[ 3] , 
       month = date[ 4] + date[ 5] , 
       day  = date[ 6] + date[ 7] , 
       hour = date[ 8] + date[ 9] , 
       minute = date[10] + date[11] , 
       seconds = date[12] + date[13]  ; 

      var reformattedDate = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + seconds; 

      var newDate = new Date(reformattedDate); 

      return newDate; 
     }; 
    }); 

//----------------------------------------------------------------------------------------------------------------------------------------------------------------- 
//----------------------------------------------------------------------- $IndexController ---------------------------------------------------------------------- 
//----------------------------------------------------------------------------------------------------------------------------------------------------------------- 

MyApp_Sim_Web.controller('IndexController' , ['$rootScope' , '$scope' , '$log' , '$location' , '$sce' , 'DB_Services' , function($rootScope , $scope , $log , $location , $sce , DB_Services) { 


     // Following declaration is aimed to enable access to DB from any controller. 

     $rootScope.Handle_DB_Request = function(p_Query , p_Callback) { 
      DB_Services(p_Query).then(function(d) { 

       p_Callback(d) ; 
      }); 
     };    

} ]) ; 

縮小的版本使用的YUICompressor是:

var MyApp_Sim_Web=angular.module("MyApp_Sim_Web",["ngRoute","ngSanitize"]);MyApp_Sim_Web.config(function(a){a.when("/Login",{templateUrl:"Pages/Login.html",controller:"LoginController"}).when("/",{templateUrl:"Pages/Login.html",controller:"LoginController"}).when("/User_Main",{templateUrl:"Pages/User_Main.html",controller:"UserController"}).otherwise({redirectTo:"/"})});MyApp_Sim_Web.filter("Make_Timestamp_Readable",function(){return function(g){var a=new String(g),e=a[0]+a[1]+a[2]+a[3],d=a[4]+a[5],f=a[6]+a[7],c=a[8]+a[9],b=a[10]+a[11],i=a[12]+a[13];var j=e+"-"+d+"-"+f+" "+c+":"+b+":"+i;var h=new Date(j);return h}});MyApp_Sim_Web.controller("IndexController",["$rootScope","$scope","$log","$location","$sce","DB_Services",function(b,d,e,f,c,a){b.Handle_DB_Request=function(h,g){a(h).then(function(i){g(i)})}}]); 

和同樣使用谷歌的編譯器:

var MyApp_Sim_Web=angular.module("MyApp_Sim_Web",["ngRoute","ngSanitize"]);MyApp_Sim_Web.config(function(a){a.when("/Login",{templateUrl:"Pages/Login.html",controller:"LoginController"}).when("/",{templateUrl:"Pages/Login.html",controller:"LoginController"}).when("/User_Main",{templateUrl:"Pages/User_Main.html",controller:"UserController"}).otherwise({redirectTo:"/"})}); 
MyApp_Sim_Web.filter("Make_Timestamp_Readable",function(){return function(a){a=new String(a);return new Date(a[0]+a[1]+a[2]+a[3]+"-"+(a[4]+a[5])+"-"+(a[6]+a[7])+" "+(a[8]+a[9])+":"+(a[10]+a[11])+":"+(a[12]+a[13]))}});MyApp_Sim_Web.controller("IndexController",["$rootScope","$scope","$log","$location","$sce","DB_Services",function(a,d,e,f,g,b){a.Handle_DB_Request=function(a,c){b(a).then(function(a){c(a)})}}]); 

錯誤我得到(Chome的控制檯)是:

[$injector:modulerr] http://errors.angularjs.org/1.4.0-rc.1/$injector/modulerr?p0=PayPlus_Sim_We…2F%2F127.0.0.1%3A59561%2FPublic_Libs%2FAngular%2Fangular.min.js%3A39%3A416) 

這是很奇怪的,因爲,如前所述,如果沒有應用程序完美的作品(在所有的任何種類的控制檯沒有錯誤)的縮小。

沒有人有任何想法是怎麼回事?

在此先感謝。

+0

'MyApp_Sim_Web.config(函數($ routeProvider){'==>'MyApp_Sim_Web.config ([「$ routeProvider」,函數($ routeProvider){' – Tushar

+0

的答案提供瞭解決方案,但沒有解釋爲什麼。角的DEP系統從功能解析'的ToString()'黑客了命名的參數。它然後使用物理參數名稱在您的函數定義中查找dep。當您縮小它時重命名函數參數意味着角嘗試尋找一個不存在的依賴。使用數組表示法對其進行別名,所以角度通過數組值非參數名稱來查找代表。所以每一個你注入東西的地方你都應該使用數組表示法。 – ste2425

+0

非常感謝@ ste2425的解釋。不幸的是,這是不可能的評論,否則我會。 – FDavidov

回答

2

究竟喜歡你注入您的控制器上的依賴關係:

MyApp_Sim_Web.controller('IndexController' , ['$rootScope' , '$scope' , '$log' , '$location' , '$sce' , 'DB_Services' , function($rootScope , $scope , $log , $location , $sce , DB_Services) 

在你的配置:

MyApp_Sim_Web.config(['$routeProvider',function($routeProvider) { 

} 
+0

謝謝@geckob提供的正確答案。它解決了這個問題(不知何故,我錯過了我沒有正確注入這個依賴的事實)。 – FDavidov

+0

@FDavidov:沒問題。我認爲這兩種方式都很好。但是如果你需要縮小,由於AngularJS DI的工作方式,你需要這樣做。它隱式地從函數參數名稱中注入,當它被縮小時它會變得混亂,因爲它會將名稱更改爲其他更短的名稱 – geckob

1

此時應更換

MyApp_Sim_Web.config(function($routeProvider) { 

有:

MyApp_Sim_Web.config(['$routeProvider',function($routeProvider) { 

我有同樣的錯誤而回,閱讀更多信息 https://stackoverflow.com/a/37197114/4937709

1

這是一個內置的問題在Angular中。 「解決方案」的討論見here