BT101 BT101 - 1 year ago 36
AngularJS Question

How to protect angularJS controller from minify

So I know that I need to use

[]
to secure my code before minification. For example:

app.controller('mainController', ['$scope', function($scope) {
$scope.message = 'HOORAY!';
}]);


But how to do that when I am not using
app
as global variable, I've got

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


How to prevent it from problems during minification?

Answer Source

The same way:

.controller('loginCtrl', ['$scope', 'authService', Controller]);

I strongly advise you to use ng-annotate, which allows using the simple syntax, and transforms it into minifiable code for you. That will make your code simpler, easier to read, and avoid a whole lot of bugs.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download