8

我正在使用 Cassette,它使用 Microsoft Ajax Minifier 来缩小 JS。这个 minifier 重命名变量,包括对 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);
        }
    });
4

2 回答 2

17

为了防止代码压缩器破坏您的 Angular 应用程序,您必须使用数组语法来定义控制器。

看看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) {
...
}]);
于 2013-11-28T12:42:20.723 回答
1

我不确定 Cassette 何时添加了此内容,但是当您创建一个捆绑包时,您可以使用它AddMinified来指示文件已尽可能缩小而不会破坏它(它在提供时不会被缩小)。

话虽如此,使用 angular 的数组语法要好得多,因为较小的文件更好!

于 2014-01-16T14:48:44.493 回答