Where Is My Angularjs Minify Error? Error $injector
I'm running into that annoying Angular minify problem (I really hope this issue is non-existent in Angular 2) I've commented out all my app module injections and going down the lis
Solution 1:
When you minify code, it minify all code, so your
controller : function($scope) {
was minified to something like
controller : function(e) {
so, just use
controller : ["$scope", function($scope) { ... }]
Solution 2:
When minifying javascript the parameter names are changed but strings remain the same. You have 2 ways that you can define which services need to be injected:
Inline Annotation:
phonecatApp.controller('PhoneListCtrl', ['$scope', '$http', function($scope, $http){ ... }]);
Using the
$inject
property:phonecatApp.controller('PhoneListCtrl', PhoneListCtrl); PhoneListCtrl.$inject = ['$scope', '$http']; functionPhoneListCtrl($scope, $http){ ... }
Using $inject
is considered more readable than inline annotations. It is best practice to always have one line declaring the controller
, service
, or directive
, one line for defining the injection values, and finally the implementation method. The method may be defined last due to the hoisting nature of javascript.
Remember: The order of your annotation (strings) and your function parameters must be the same!
Post a Comment for "Where Is My Angularjs Minify Error? Error $injector"