如何保护 angularJS 控制器不被缩小
How to protect angularJS controller from minify
所以我知道在缩小之前我需要使用 []
来保护我的代码。例如:
app.controller('mainController', ['$scope', function($scope) {
$scope.message = 'HOORAY!';
}]);
但是当我不使用 app
作为全局变量时如何做到这一点,我有
(function () {
'use strict';
angular
.module('app')
.controller('loginCtrl', Controller);
function Controller($scope, authService) {
var vm = $scope;
vm.login = function(login_field, password_field) {
var loginData = {
login: login_field,
password: password_field
};
authService.login(loginData);
};
}
})();
如何防止缩小时出现问题?
同理:
.controller('loginCtrl', ['$scope', 'authService', Controller]);
我强烈建议您使用 ng-annotate,它允许使用简单的语法,并为您将其转换为可缩小的代码。这将使您的代码更简单、更易于阅读,并避免大量错误。
我相信应该是这样的:
(function () {
'use strict';
angular
.module('app')
.controller('loginCtrl', ['$scope', 'authService', function($scope, authService) {
$scope.login = function(login_field, password_field) {
var loginData = {
login: login_field,
password: password_field
};
authService.login(loginData);
};
}]);
})();
当控制器或服务是像上面代码中那样的命名函数时,用 $inject
注释看起来最好(参见 John Papa style guide)。
angular
.module('app')
.controller('loginCtrl', Controller);
Controller.$inject = ['$scope', 'authService'];
function Controller($scope, authService) {...}
提升允许将注释放在可注入函数的正上方。
您可以尝试一种方法grunt-ngmin before the minification that will searches and replace the minification with minify-friendly code. Go to this link you will see example https://github.com/btford/grunt-ngmin#example
所以我知道在缩小之前我需要使用 []
来保护我的代码。例如:
app.controller('mainController', ['$scope', function($scope) {
$scope.message = 'HOORAY!';
}]);
但是当我不使用 app
作为全局变量时如何做到这一点,我有
(function () {
'use strict';
angular
.module('app')
.controller('loginCtrl', Controller);
function Controller($scope, authService) {
var vm = $scope;
vm.login = function(login_field, password_field) {
var loginData = {
login: login_field,
password: password_field
};
authService.login(loginData);
};
}
})();
如何防止缩小时出现问题?
同理:
.controller('loginCtrl', ['$scope', 'authService', Controller]);
我强烈建议您使用 ng-annotate,它允许使用简单的语法,并为您将其转换为可缩小的代码。这将使您的代码更简单、更易于阅读,并避免大量错误。
我相信应该是这样的:
(function () {
'use strict';
angular
.module('app')
.controller('loginCtrl', ['$scope', 'authService', function($scope, authService) {
$scope.login = function(login_field, password_field) {
var loginData = {
login: login_field,
password: password_field
};
authService.login(loginData);
};
}]);
})();
当控制器或服务是像上面代码中那样的命名函数时,用 $inject
注释看起来最好(参见 John Papa style guide)。
angular
.module('app')
.controller('loginCtrl', Controller);
Controller.$inject = ['$scope', 'authService'];
function Controller($scope, authService) {...}
提升允许将注释放在可注入函数的正上方。
您可以尝试一种方法grunt-ngmin before the minification that will searches and replace the minification with minify-friendly code. Go to this link you will see example https://github.com/btford/grunt-ngmin#example