<br />
<b>Warning</b>:  Undefined array key "global_protection_id" in <b>/home/wikitechy/public_html/interview-questions/wp-content/plugins/content-protector/inc/class-ps-rest-handler.php</b> on line <b>51</b><br />
{"id":99,"date":"2021-07-10T19:41:48","date_gmt":"2021-07-10T19:41:48","guid":{"rendered":"https:\/\/www.wikitechy.com\/interview-questions\/?p=99"},"modified":"2021-09-15T06:24:38","modified_gmt":"2021-09-15T06:24:38","slug":"what-is-dependency-injection-in-angularjs","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/interview-questions\/angularjs\/what-is-dependency-injection-in-angularjs\/","title":{"rendered":"What is Dependency injection in AngularJS ?"},"content":{"rendered":"<div class=\"TextHeading\">\n<div class=\"hddn\">\n<h2 id=\"dependency-injection-in-angularjs\" class=\"color-green\" style=\"text-align: justify;\">Dependency injection in AngularJS<\/h2>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li>Dependency Injection is a software design pattern in which an object is given its dependencies, rather than the object creating them itself. Design pattern allows us to remove the hard-coded dependencies and making it possible to change them whenever needed.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"TextHeading\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<h2 id=\"why-do-you-need-to-inject-dependencies\" class=\"color-green\">Why do you need to inject dependencies ?<\/h2>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li>Separate the process of creation and consumption of dependencies.<\/li>\n<li>Let the consumer worry only about how to use the dependency, and leave the process of creation of the dependency to somebody else.<\/li>\n<li>Allow concurrent\/independent development of the dependency and the dependent entity, while only maintaining a known contract.<\/li>\n<li>Facilitate changing of the dependencies when needed.<\/li>\n<li>Allow injecting mock objects as dependencies for testing, by maintaining the agreed contract.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"text-center row\" style=\"text-align: justify;\">\n<div class=\"col-sm-12\">\n<div id=\"bsa-zone_1590522538159-8_123456\"><span style=\"font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';\">There are only three ways a component (object or function) can get a hold of its dependencies:<\/span><\/div>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>The component can create the dependency, typically using the new operator.<\/li>\n<li>The component can look up the dependency, by referring to a global variable.<\/li>\n<li>The component can have the dependency passed to it where it is needed.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li>The first two options of creating or looking up dependencies are not optimal because they hard code the dependency to the component. This makes it difficult, if not impossible, to modify the dependencies. This is especially problematic in tests, where it is often desirable to provide mock dependencies for test isolation.<\/li>\n<li>The third option is the most viable, since it removes the responsibility of locating the dependency from the component. The dependency is simply handed to the component.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"subheading\" style=\"text-align: justify;\">\n<h2 id=\"example-1\">Example 1<\/h2>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">function SomeClass(greeter) <br\/>{<br\/>this.greeter = greeter;<br\/>}<br\/>SomeClass.prototype.doSomething = function(name) <br\/>{<br\/>  this.greeter.greet(name);<br\/>}<\/code><\/pre> <\/div>\n<\/div>\n<p style=\"text-align: justify;\">This is desirable, but it puts the responsibility of getting hold of the dependency on the code that constructs Some Class.<\/p>\n<div class=\"ImageContent\" style=\"text-align: justify;\">\n<div class=\"hddn\"><img decoding=\"async\" class=\"img-responsive center-block aligncenter\" src=\"https:\/\/cdn.wikitechy.com\/interview-questions\/angularjs\/dependency-injection.png\" alt=\" dependency-injection\" \/><\/div>\n<\/div>\n<p style=\"text-align: justify;\" align=\"center\">\n<p style=\"text-align: justify;\">To manage the responsibility of dependency creation, each AngularJS application has an injector. The injector is a service locator that is responsible for construction and lookup of dependencies.<\/p>\n<div style=\"text-align: justify;\">\n<div id=\"bsa-zone_1590522394795-6_123456\"><\/div>\n<\/div>\n<div class=\"TextHeading\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<h2 id=\"using-the-injector-service\" class=\"color-green\">Using the injector service:<\/h2>\n<\/div>\n<\/div>\n<div class=\"subheading\" style=\"text-align: justify;\">\n<h2 id=\"syntax\">Syntax :<\/h2>\n<\/div>\n<div class=\"CodeContent\" style=\"text-align: justify;\">\n<div class=\"hddn\"><\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">\/\/ Provide the wiring information in a module<br\/>var myModule = angular.module(&#039;myModule&#039;, []);<\/code><\/pre> <\/div>\n<p>Explain the injector how to build a greeter service. Notice that greeter is dependent on the $window service. The greeter service is an object that contains a greet method.<\/p>\n<\/div>\n<\/div>\n<div class=\"subheading\" style=\"text-align: justify;\">\n<h2 id=\"example-2\">Example 2<\/h2>\n<\/div>\n<div class=\"CodeContent\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<figure class=\"highlight\"><div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">myModule.factory(&#039;greeter&#039;, function($window) <br\/>{<br\/>  return <br\/>  {<br\/>    greet: function(text) <br\/>    {<br\/>      $window.alert(text);<br\/>    }<br\/>  };<br\/>};<\/code><\/pre> <\/div><\/figure>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Answer : Dependency Injection is a software design pattern in which an object&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"passster_activate_protection":false,"passster_protect_child_pages":"","passster_protection_type":"password","passster_password":"","passster_activate_overwrite_defaults":"","passster_headline":"","passster_instruction":"","passster_placeholder":"","passster_button":"","passster_id":"","passster_activate_misc_settings":"","passster_redirect_url":"","passster_hide":"no","passster_area_shortcode":"","gtb_hide_title":false,"gtb_wrap_title":false,"gtb_class_title":"","gtb_remove_headerfooter":false,"footnotes":""},"categories":[45],"tags":[54,72,57,52,56,82,55,53,75,73,76,84,80,79,83,77,81,70,74,71,78],"class_list":["post-99","post","type-post","status-publish","format-standard","hentry","category-angularjs","tag-advanced-angularjs-interview-questions","tag-angular-inject-service-into-controller","tag-angularjs-2-interview-questions-and-answers-for-experienced","tag-angularjs-coding-interview-questions","tag-angularjs-coding-test","tag-angularjs-dependency-injection-example","tag-angularjs-interview-questions-codeproject","tag-angularjs-tricky-interview-questions","tag-angularjs-tricky-interview-questionsa","tag-dependency-injection-angular-2","tag-dvanced-angularjs-interview-questions","tag-factory-method-in-angularjs","tag-how-angularjs-works-internally","tag-injector-in-angularjs","tag-provider-angularjs","tag-what-is-angularjs","tag-what-is-dependency-injection-in-angularjs","tag-which-components-can-be-injected-as-a-dependency-in-angularjs-mcq","tag-which-of-provider-type-can-be-injected-during-config-phase","tag-which-ones-are-singleton-in-angularjs","tag-why-use-angularjs"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>What is Dependency injection in AngularJS ? - AngularJS<\/title>\n<meta name=\"description\" content=\"What is Dependency injection in AngularJS ? - AngularJs basic interview question - Dependency Injection is a software design pattern\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wikitechy.com\/interview-questions\/angularjs\/what-is-dependency-injection-in-angularjs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is Dependency injection in AngularJS ? - AngularJS\" \/>\n<meta property=\"og:description\" content=\"What is Dependency injection in AngularJS ? - AngularJs basic interview question - Dependency Injection is a software design pattern\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/angularjs\/what-is-dependency-injection-in-angularjs\/\" \/>\n<meta property=\"og:site_name\" content=\"Wikitechy\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-10T19:41:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-09-15T06:24:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdn.wikitechy.com\/interview-questions\/angularjs\/dependency-injection.png\" \/>\n<meta name=\"author\" content=\"Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Editor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/angularjs\/what-is-dependency-injection-in-angularjs\/\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/angularjs\/what-is-dependency-injection-in-angularjs\/\",\"name\":\"What is Dependency injection in AngularJS ? - AngularJS\",\"isPartOf\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/angularjs\/what-is-dependency-injection-in-angularjs\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/angularjs\/what-is-dependency-injection-in-angularjs\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdn.wikitechy.com\/interview-questions\/angularjs\/dependency-injection.png\",\"datePublished\":\"2021-07-10T19:41:48+00:00\",\"dateModified\":\"2021-09-15T06:24:38+00:00\",\"author\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\"},\"description\":\"What is Dependency injection in AngularJS ? - AngularJs basic interview question - Dependency Injection is a software design pattern\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wikitechy.com\/interview-questions\/angularjs\/what-is-dependency-injection-in-angularjs\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/angularjs\/what-is-dependency-injection-in-angularjs\/#primaryimage\",\"url\":\"https:\/\/cdn.wikitechy.com\/interview-questions\/angularjs\/dependency-injection.png\",\"contentUrl\":\"https:\/\/cdn.wikitechy.com\/interview-questions\/angularjs\/dependency-injection.png\"},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/\",\"name\":\"Wikitechy\",\"description\":\"Interview Questions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\",\"name\":\"Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"caption\":\"Editor\"},\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What is Dependency injection in AngularJS ? - AngularJS","description":"What is Dependency injection in AngularJS ? - AngularJs basic interview question - Dependency Injection is a software design pattern","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wikitechy.com\/interview-questions\/angularjs\/what-is-dependency-injection-in-angularjs\/","og_locale":"en_US","og_type":"article","og_title":"What is Dependency injection in AngularJS ? - AngularJS","og_description":"What is Dependency injection in AngularJS ? - AngularJs basic interview question - Dependency Injection is a software design pattern","og_url":"https:\/\/www.wikitechy.com\/interview-questions\/angularjs\/what-is-dependency-injection-in-angularjs\/","og_site_name":"Wikitechy","article_published_time":"2021-07-10T19:41:48+00:00","article_modified_time":"2021-09-15T06:24:38+00:00","og_image":[{"url":"https:\/\/cdn.wikitechy.com\/interview-questions\/angularjs\/dependency-injection.png"}],"author":"Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Editor","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wikitechy.com\/interview-questions\/angularjs\/what-is-dependency-injection-in-angularjs\/","url":"https:\/\/www.wikitechy.com\/interview-questions\/angularjs\/what-is-dependency-injection-in-angularjs\/","name":"What is Dependency injection in AngularJS ? - AngularJS","isPartOf":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/angularjs\/what-is-dependency-injection-in-angularjs\/#primaryimage"},"image":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/angularjs\/what-is-dependency-injection-in-angularjs\/#primaryimage"},"thumbnailUrl":"https:\/\/cdn.wikitechy.com\/interview-questions\/angularjs\/dependency-injection.png","datePublished":"2021-07-10T19:41:48+00:00","dateModified":"2021-09-15T06:24:38+00:00","author":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757"},"description":"What is Dependency injection in AngularJS ? - AngularJs basic interview question - Dependency Injection is a software design pattern","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wikitechy.com\/interview-questions\/angularjs\/what-is-dependency-injection-in-angularjs\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/angularjs\/what-is-dependency-injection-in-angularjs\/#primaryimage","url":"https:\/\/cdn.wikitechy.com\/interview-questions\/angularjs\/dependency-injection.png","contentUrl":"https:\/\/cdn.wikitechy.com\/interview-questions\/angularjs\/dependency-injection.png"},{"@type":"WebSite","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website","url":"https:\/\/www.wikitechy.com\/interview-questions\/","name":"Wikitechy","description":"Interview Questions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757","name":"Editor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","caption":"Editor"},"url":"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/"}]}},"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/99","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/comments?post=99"}],"version-history":[{"count":4,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/99\/revisions"}],"predecessor-version":[{"id":3816,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/99\/revisions\/3816"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/media?parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/categories?post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/tags?post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}