2

I am trying to uglify my AngularJS Code. I followed all the tutorials on the internet an have now the following code but I am still getting and error after uglifying:

var app = angular.module('myapp', ['ngMaterial', 'pascalprecht.translate', 'ngSanitize', 'material.components.expansionPanels', 'ngFileSaver', 'ngMessages', 'ngFileUpload', 'ngStorage']

TestController.$inject = ['$scope', '$translate', '$timeout','$mdSidenav', '$log', '$sessionStorage'];

angular.module('myapp').controller('TestController', TestController);

function TestController ($scope, $translate, $timeout, $mdSidenav, $log, $sessionStorage) {}

Edit: I have found the problem. The problem was I configurated the module with:

app.config(function ($locationProvider) {
    $locationProvider.html5Mode(true);
});

Rather than:

app.config(['$locationProvider', function ($locationProvider) {
    $locationProvider.html5Mode(true);
}]);

app.directive('onReadFile', function ($parse) {
    return {
        restrict: 'A',
        scope: false,
        link: function(scope, element, attrs) {
            var fn = $parse(attrs.onReadFile);

            element.on('change', function(onChangeEvent) {
                var reader = new FileReader();

                reader.onload = function(onLoadEvent) {
                    scope.$apply(function() {
                        fn(scope, {$fileContent:onLoadEvent.target.result});
                    });
                };

                reader.readAsText((onChangeEvent.srcElement || onChangeEvent.target).files[0]);
            });
        }
    };
});
12
  • Is the name of your module myapp (all lower case) or myApp (camel case)? Commented Sep 15, 2016 at 15:19
  • Syntax is not an issue here! Commented Sep 15, 2016 at 15:26
  • Considering you have an issue with the injector...it is. There's not quite enough information here to say beyond a shadow of a doubt what it is; could you show us what services you depend on in your module? Commented Sep 15, 2016 at 15:29
  • I have just edited it. You can now see the dependencies! Commented Sep 15, 2016 at 15:33
  • Could you also add the exact error you're getting after the code? It's tough to say which module is going awry here. Commented Sep 15, 2016 at 15:34

2 Answers 2

-1

Always try and make sure your app is running correctly before trying to uglify the code.

Same as you were doing with the controller, when setting up your directives use the same approach where you use $inject to declare the dependencies.

See below

app.directive('onReadFile',onReadFile);

onReadFile.$inject = ['$parse'];

function onReadFile($parse){

  var directive = {
    restrict:'A',
    scope: false,
    link: link
  }

  return directive;

  //////////

  function link(scope, element, attrs){

      var fn = $parse(attrs.onReadFile); 

      element.on('change', function(onChangeEvent) { 

        var reader = new FileReader(); 

        reader.onload = function(onLoadEvent) { 
          scope.$apply(function() { 
            fn(scope, {$fileContent:onLoadEvent.target.result}); 
          }); 
        }; 

        reader.readAsText((onChangeEvent.srcElement || onChangeEvent.target).files[0]); 
      }); 

  }// end link

}// onReadFile  
Sign up to request clarification or add additional context in comments.

3 Comments

The function is hoisted already and doesn't really do anything until Angular invokes it, so I'm not 100% convinced it's declaration order...
@Makoto but order could be important if we want to uglify.
@valverde93: No, order isn't important. Again, the fact that the function is declared as function ... will hoist it within its scope, so it's going to be made readily available.
-1

Possible duplicate of this question.

Try using it this way:

angular.module('myapp').controller('TestController', ['$scope', '$translate', '$timeout','$mdSidenav', '$log', '$sessionStorage', function checkInCtrl ($scope, $translate, $timeout, $mdSidenav, $log, $sessionStorage){}]);

2 Comments

Both forms of dependency injection are valid and equivalent, and would survive uglification.
Somehow my form does not survive it! @Makoto