简体   繁体   English

使用AngularJS缩小Javascript-依赖注入错误

[英]Minifying Javascript using AngularJS - Dependancy Injection error

I have a SPA application working perfectly so far. 到目前为止,我有一个SPA应用程序可以正常运行。 It had been developed in JavaScript using AngularJS (+ other libs). 它是使用AngularJS(及其他库)在JavaScript中开发的。

Now, I want to minify the scripts and I'm testing both yuicompressor and Google's compiler. 现在,我想缩小脚本,同时测试yuicompressor和Google的编译器。

As soon as I deploy the minified version of the script and test it, I get an error. 一旦部署了脚本的精简版并进行了测试,就会收到错误消息。

The JavaScript file prior to minifying is: 缩小之前的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) ;
            });
        };            

} ]) ;

The minified version using yuicompressor is: 使用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)})}}]);

and the same using Google's compiler is: 使用Google的编译器的结果是:

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)})}}]);

The error I get (Chome's console) is: 我收到的错误(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)

It is quite strange since, as stated, without the minification the application works perfectly (no error at all of any kind in console). 如前所述,这很奇怪,因为如果不进行精简,应用程序将完美运行(控制台中的任何类型都没有错误)。

Does anyone have any idea what is going on? 有人知道发生了什么吗?

Thanks in advance. 提前致谢。

Do exactly like you injected dependencies on your controller: 就像在控制器上注入依赖项一样:

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

In your config: 在您的配置中:

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

}

You should replace 你应该更换

MyApp_Sim_Web.config(function($routeProvider) {

with : 与:

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

I had the same error a while back, read this for more info https://stackoverflow.com/a/37197114/4937709 我前一段时间有相同的错误,请阅读此内容以获取更多信息https://stackoverflow.com/a/37197114/4937709

This is a built-in problem in Angular. 这是Angular中的一个内置问题。 The "solutions" are discussed here . 这里讨论“解决方案”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM