{"id":17330,"date":"2015-02-17T13:25:29","date_gmt":"2015-02-17T07:55:29","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=17330"},"modified":"2017-08-03T14:15:18","modified_gmt":"2017-08-03T08:45:18","slug":"minification-safe-angular-js","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/minification-safe-angular-js\/","title":{"rendered":"Minification safe Angular.js"},"content":{"rendered":"<p>Recently, I wanted\u00a0to <a title=\"Grails Version Upgrade to 2.4.3\" href=\"http:\/\/www.tothenew.com\/grails-application-development\">upgrade my Grails\u00a0application to 2.4.3<\/a> and also switched from resource plugin to assets-pipeline plugin. As everything was going\u00a0good in the development, so I pushed it to QA after which I was bombarded with JavaScript errors and missing providers.<\/p>\n<p>What has happened?? So here, <a title=\"Angular.js Development\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">Angular.js<\/a> internal dependency injection was broken\u2026 \ud83d\ude41<\/p>\n<p>Since <a title=\"Building Intuitive Frontend Interfaces with AngularJS\" href=\"http:\/\/www.tothenew.com\/blog\/building-intuitive-frontend-interfaces-with-angularjs\/\">Angular infers the controller&#8217;s dependencies<\/a> from the names of arguments to the controller&#8217;s constructor function, if you were to minify the JavaScript code for controller, all of its function arguments would be minified as well, and the dependency injector would not be able to identify services correctly.<\/p>\n<p>Solution for the problem, \u00a0&#8216;annotations&#8217; \u2013 an array that contains the names of the dependency a function needs, and the function itself.<\/p>\n<p>By annotating the function with the names of the dependencies, provided as strings, which will not get minified.<\/p>\n<p>There are two ways to provide these injection annotations:<\/p>\n<p>1. Create a $inject property on the controller function which holds an array of strings. Each string in the array is the name of the service to inject for the corresponding parameter. we would write:<\/p>\n<p>[code]<br \/>\nvar myController = function ($scope, $http) {\/\/your code}<br \/>\nmyController.$inject = [&#8216;$scope&#8217;, &#8216;$http&#8217;];<br \/>\n[\/code]<\/p>\n<p>2. Use an inline annotation where, instead of just providing the function, you provide an array. This array contains a list of the service names, followed by the function itself.<\/p>\n<p>[code]<br \/>\nfunction myController ($scope, $http) {\/\/your code}<br \/>\nmyApp.controller(&#8216;myController&#8217;, [&#8216;$scope&#8217;, &#8216;$http&#8217;, myController]);<br \/>\n[\/code]<\/p>\n<p>Second method is a\u00a0common one if you want to provide anonymous function like:<\/p>\n<p>[code]<br \/>\nmyApp.controller(&#8216;myController&#8217;, [&#8216;$scope&#8217;, &#8216;$http&#8217;, function ($scope, $http){<br \/>\n\/\/your controller code<br \/>\n}]);<br \/>\n[\/code]<\/p>\n<p>What about services, directives and factories.\u00a0Let us see the examples below:<\/p>\n<p>For modules:<\/p>\n<p>[code]<br \/>\nvar myModule = angular.module(&#8216;myModule&#8217;,<br \/>\n[ &#8216;$scope&#8217;, &#8216;$http&#8217; \/*Dependencies as an array of strings.*\/])<br \/>\n[\/code]<\/p>\n<p>For services:<\/p>\n<p>[code]<\/p>\n<p>myModule.service(&#8216;serviceName&#8217;, [&#8216;$scope&#8217;, &#8216;$http&#8217;, function($scope, $http) {<br \/>\n\/\/your code<br \/>\n}])<br \/>\n[\/code]<\/p>\n<p>For directives:<\/p>\n<p>[code]<br \/>\nmyModule.directive(&#8216;myDirective&#8217;, [&#8216;$scope&#8217;, &#8216;$http&#8217;, function($scope, $http) {<br \/>\n\/\/your code<br \/>\n}])<br \/>\n[\/code]<\/p>\n<p>For factories:<\/p>\n<p>[code]<br \/>\nmyModule.factory(&#8216;myFactory&#8217;, [&#8216;$scope&#8217;, &#8216;$http&#8217;, function($scope, $http) {<br \/>\n\/\/your code<br \/>\n}])<br \/>\n[\/code]<\/p>\n<p>Using\u00a0inline annotation is preferred and\u00a0mentioned in angular <a href=\"https:\/\/docs.angularjs.org\/guide\/di\">dependency injection<\/a> docs as well. By simply following above code convention you&#8217;ll never have to worry about minification surprises.<\/p>\n<p><span style=\"font-size: 20px; color: blue;\">Read Further on AngularJS Series<\/span><\/p>\n<ul>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-a-write-less-do-more-javascript-framework\/\">AngularJS :\u00a0A \u201cWrite Less Do More\u201d JavaScript Framework<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-updating-a-label-dynamically-with-user-input\/\">AngularJS :\u00a0Updating a Label Dynamically with User Input<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-adding-items-to-a-javascript-list-and-updating-corresponding-dom-dynamically\/\">AngularJS :\u00a0Adding Items to a JavaScript List and updating corresponding DOM Dynamically<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-text-suggestions\/\">AngularJS :\u00a0Text Suggestions<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-sorting-objects-on-various-attributes\/\">AngularJS :\u00a0Sorting objects on various attributes<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-fetching-data-from-the-server\/\">AngularJS :\u00a0Fetching data from the server<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-multiple-views-layout-template-and-routing\/\">AngularJS :\u00a0Multiple Views, Layout\u00a0Template\u00a0and Routing<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-implementing-routes-and-multiple-views\/\">AngularJS :\u00a0Implementing Routes And Multiple Views<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/building-intuitive-frontend-interfaces-with-angularjs\/\">Building Intuitive Frontend Interfaces with AngularJS<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Recently, I wanted\u00a0to upgrade my Grails\u00a0application to 2.4.3 and also switched from resource plugin to assets-pipeline plugin. As everything was going\u00a0good in the development, so I pushed it to QA after which I was bombarded with JavaScript errors and missing providers. What has happened?? So here, Angular.js internal dependency injection was broken\u2026 \ud83d\ude41 Since Angular [&hellip;]<\/p>\n","protected":false},"author":121,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":5},"categories":[1439,3429],"tags":[3955,1186,4697,1642,4840,1641,1570],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/17330"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/121"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=17330"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/17330\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=17330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=17330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=17330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}