{"id":20803,"date":"2015-06-15T10:56:14","date_gmt":"2015-06-15T05:26:14","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=20803"},"modified":"2017-08-03T15:51:36","modified_gmt":"2017-08-03T10:21:36","slug":"dependency-annotation-in-angularjs","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/dependency-annotation-in-angularjs\/","title":{"rendered":"Dependency Annotation in AngularJS"},"content":{"rendered":"<p>When a function of service or controller invoked via the injector then we have to annotate these functions so that the injector knows what service to inject into the function.<\/p>\n<p>In <a title=\"Angular Development\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">Angular<\/a>, there are three ways of annotating code with service name &#8211;<\/p>\n<ul>\n<li>Inline Array Annotation<\/li>\n<li>$inject Property Annotation<\/li>\n<li>Implicit Annotation<\/li>\n<\/ul>\n<ol>\n<li>\n<h3>Inline Array Annotation<\/h3>\n<p>This is most preferred way to annotate components in Angular. Lets take an example of registering a controller to module.<\/p>\n<p>[java]<br \/>\nvar myApp = angular.module(&quot;myApp&quot;, []);<\/p>\n<p>myApp.controller(&quot;MyController&quot;, [&quot;$scope&quot;, &quot;MyService&quot;, function($scope, MyService) {<br \/>\n\t$scope.myService = MyService;<br \/>\n}]);<br \/>\n[\/java]<\/p>\n<p>In the above example, we use an array whose elements are type of string and the last one is the function. In the array, those strings are names of the dependencies. be careful when use this method to keep the annotation array sync with the parameters in the function declaration.<\/li>\n<li>\n<h3>$inject Property Annotation<\/h3>\n<p>If your code allow to minifiers to rename the function parameters and still to inject the right services then you can use $inject property which is an array of dependency names to inject to the function.<\/p>\n<p>[java]<br \/>\nvar myApp = angular.module(&quot;myApp&quot;, []);<\/p>\n<p>var MyController = function($scope, MyService) {<br \/>\n\t$scope.myService = MyService;<br \/>\n};<br \/>\nMyController.$inject = [&quot;$scope&quot;, &quot;MyService&quot;];<\/p>\n<p>myApp.controller(&quot;MyController&quot;, MyController);<br \/>\n[\/java]<\/p>\n<p>In the above example, we use an array whose elements are type of string. In the array, those strings are names of the dependencies. same as \u2018Inline Array Annotation\u2019 be careful to keep the array sync with the parameters in the function declaration.<\/li>\n<li>\n<h3>Implicit Annotation<\/h3>\n<p>This way is the simplest one but if you are plan to minify your code then avoid it because your service names will get renamed and services are not available to the function. In this, function parameters are treated as names of services to inject into the function.<\/p>\n<p>[java]<br \/>\nvar myApp = angular.module(&quot;myApp&quot;, []);<\/p>\n<p>myApp.controller(&quot;MyController&quot;, function($scope, MyService) {<br \/>\n\t$scope.myService = MyService;<br \/>\n});<br \/>\n[\/java]<\/p>\n<p>In the above example \u201c$scope\u201d and \u201cMyService\u201d are treated as name of the services to inject to the \u201cMyController\u201d.<\/p>\n<p>No need to sync anything with the function parameters because here is no array of names so you can also rearrange dependencies in the function declaration.<\/p>\n<p><strong>NOTE: This method will not work with Javascript minifiers\/obfuscators (source or machine code that is difficult for humans to understand.) because they rename the parameters.<\/strong><\/li>\n<\/ol>\n<p>Hope this helps !!!<\/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>When a function of service or controller invoked via the injector then we have to annotate these functions so that the injector knows what service to inject into the function. In Angular, there are three ways of annotating code with service name &#8211; Inline Array Annotation $inject Property Annotation Implicit Annotation Inline Array Annotation This [&hellip;]<\/p>\n","protected":false},"author":103,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[1439,3429],"tags":[3955,955,4697,1866,1865],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/20803"}],"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\/103"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=20803"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/20803\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=20803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=20803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=20803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}