缩小是破坏我的AngularJs代码

Dav*_*vid 8 minify angularjs cassette

我正在使用Cassette,它使用Microsoft Ajax Minifier来缩小JS.此缩小器重命名变量,包括对Angular具有特殊含义的变量,例如$scope$http.所以Cassette打破了我的Angular代码!

我怎样才能防止这种情况发生?

作为参考,这是正在被破坏的Angular代码.该$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 /[^\s@]+@[^\s@]+\.[^\s@]+/.test(emailAddress);
        }
    });
Run Code Online (Sandbox Code Playgroud)

pea*_*man 17

要防止代码缩减器破坏角度应用程序,必须使用数组语法来定义控制器.

请查看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) {
...
}]);
Run Code Online (Sandbox Code Playgroud)

  • 我并不是说过度苛刻,而是拯救了我的生命和全人类的未来. (15认同)