0

When I try to minify the following code it breaks. What's wrong? What's the simplest way to fix this?

(function () {
    'use strict';
    angular.module('weatherapp.weatherlist')
    .controller('WeatherlistController2', function($scope, utcFactory) { 
        var vm = this; 
        vm.utc = utcFactory.myUTC(); 
     })

    .factory('utcFactory', function() {
        var myUTC = function() {
          var offset = -new Date().getTimezoneOffset();
          var utc = ((offset > 0 ? '+' : '') + offset / 60);
          return utc;
        }

        return {
          myUTC: myUTC
        }
    });
})();
1
  • First, what errors? Also, why are you injecting $scope if you're not even using it? Commented Jul 12, 2016 at 22:36

1 Answer 1

1

This is a common problem. Use the array syntax for your controller:

.controller('WeatherlistController2', [ '$scope', 'utcFactory', function($scope, utcFactory) {}])

See here as well: Angularjs minify best practice

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

8 Comments

Why does minification break this?
By the way @AgentZebra, there are tools that will do this for you automatically as part of your build step, if you have one. gulp-ng-annotate, for example.
Minification breaks this because if you don't use the syntax that is mentioned above, angular literally calls the .toString() method on your controller function to determine what dependencies are being injected into your controller (in this case, $scope and utcFactory). When you minify your code, $scope and utcFactory are going to be minimized into random variabled like a or b, so when angular parses your function as text, it won't be able to inject 'a' or 'b', and then your whole application won't work. See anandmanisankar.com/posts/…
Just to add: Minifiers usually have a name mangling option. If that option is on, function parameters are changed to random variables like a or b ....
@AminMeyghani Thanks, I figured this one out, how do you turn the name mangler on and off?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.