2013-11-28 65 views
6

我正在使用使用Microsoft Ajax Minifier來縮小JS的Cassette。此縮小器會重命名變量,包括對Angular具有特殊含義的變量,例如$scope$http。所以Cassette打破了我的Angular代碼!Minification正在破壞我的AngularJs代碼

我該如何防止這種情況發生?

作爲參考,這是正在被破壞的角碼。該$scope$http功能參數被重命名:

// <reference path="vendor/angular.js" /> 

angular.module('account-module', []) 
    .controller('ForgottenPasswordController', function ($scope, $http) { 

     $scope.email = { 
      value: '', 
      isValid: false, 
      containerStyle: "unvalidated", 
      validate: function() { 
       var valid = isEmailAdressValid($scope.email.value); 
       $scope.email.isValid = valid; 
       $scope.email.containerStyle = valid ? "valid" : "invalid"; 
       return valid; 
      }, 
      removeErrorMessage: function() { 
       $scope.email.containerStyle = "unvalidated"; 
      } 
     }; 

     $scope.display = { 
      formClass: '', 
      congratulationsClass: 'hide' 
     }; 

     $scope.submit = function (event) { 
      event.preventDefault(); 

      var emailValid = $scope.email.validate(); 
      if (emailValid) { 
       $http({ 
        method: 'POST', 
        url: '/account/forgot-password', 
        params: { email: $scope.email.value }, 
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' } 
       }).success(function(data) { 
        $scope.success(data); 
       }).error(function() { $scope.error(); }); 
      } 
     }; 

     $scope.success = function (data) { 
      switch (data.Outcome) { 
       case 1: 
        $scope.display.formClass = "hide"; 
        $scope.display.congratulationsClass = ""; 
        break; 
       case 2: 
        $scope.email.containerStyle = "invalid"; 
        break; 
      } 
     }; 

     $scope.error = function() { 
      alert('Sorry, an error occurred.'); 
     }; 

     function isEmailAdressValid(emailAddress) { 
      return /[^\[email protected]][email protected][^\[email protected]]+\.[^\[email protected]]+/.test(emailAddress); 
     } 
    }); 
+2

您能否向我們展示您的控制器語法,然後我們可以評論您使用的是正確的語法還是錯誤的語法 –

回答

15

爲了防止代碼minifiers毀壞你的角度應用程序,您必須使用數組語法定義控制器。

http://odetocode.com/blogs/scott/archive/2013/03/13/angularjs-controllers-dependencies-and-minification.aspx

(從OP): 作爲參考,這裏是改變的代碼:

angular.module('account-module', []) 
    .controller('ForgottenPasswordController', ["$scope", "$http", function ($scope, $http) { 
... 
}]); 
+14

我並不是說過度超速,但是您拯救了我的生命和所有人類的未來。 – David

1

我不知道當盒式增加。但是,當你創建一個包你可以使用AddMinified來表示該文件被縮小,因爲它不會被破壞(它在被提供時不會被縮小)。

這就是說,使用angular的數組語法要好得多,因爲小文件更好!

+0

感謝您的潛在有用的替代方案。 – David