<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>angular inject service into controller - Wikitechy</title>
	<atom:link href="https://www.wikitechy.com/interview-questions/tag/angular-inject-service-into-controller/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.wikitechy.com/interview-questions/tag/angular-inject-service-into-controller/</link>
	<description>Interview Questions</description>
	<lastBuildDate>Wed, 15 Sep 2021 06:24:38 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://www.wikitechy.com/interview-questions/wp-content/uploads/2025/10/cropped-wikitechy-icon-32x32.png</url>
	<title>angular inject service into controller - Wikitechy</title>
	<link>https://www.wikitechy.com/interview-questions/tag/angular-inject-service-into-controller/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>What is Dependency injection in AngularJS ?</title>
		<link>https://www.wikitechy.com/interview-questions/angularjs/what-is-dependency-injection-in-angularjs/</link>
					<comments>https://www.wikitechy.com/interview-questions/angularjs/what-is-dependency-injection-in-angularjs/#respond</comments>
		
		<dc:creator><![CDATA[Editor]]></dc:creator>
		<pubDate>Sat, 10 Jul 2021 19:41:48 +0000</pubDate>
				<category><![CDATA[Angularjs]]></category>
		<category><![CDATA[advanced angularjs interview questions]]></category>
		<category><![CDATA[angular inject service into controller]]></category>
		<category><![CDATA[angularjs 2 interview questions and answers for experienced]]></category>
		<category><![CDATA[angularjs coding interview questions]]></category>
		<category><![CDATA[angularjs coding test]]></category>
		<category><![CDATA[angularjs dependency injection example]]></category>
		<category><![CDATA[angularjs interview questions codeproject]]></category>
		<category><![CDATA[angularjs tricky interview questions]]></category>
		<category><![CDATA[angularjs tricky interview questionsa]]></category>
		<category><![CDATA[dependency injection angular 2]]></category>
		<category><![CDATA[dvanced angularjs interview questions]]></category>
		<category><![CDATA[factory method in angularjs]]></category>
		<category><![CDATA[how angularjs works internally]]></category>
		<category><![CDATA[injector in angularjs]]></category>
		<category><![CDATA[provider angularjs]]></category>
		<category><![CDATA[what is angularjs]]></category>
		<category><![CDATA[what is dependency injection in angularjs]]></category>
		<category><![CDATA[which components can be injected as a dependency in angularjs mcq]]></category>
		<category><![CDATA[which of provider type can be injected during config phase]]></category>
		<category><![CDATA[which ones are singleton in angularjs?]]></category>
		<category><![CDATA[why use angularjs]]></category>
		<guid isPermaLink="false">https://www.wikitechy.com/interview-questions/?p=99</guid>

					<description><![CDATA[Answer : Dependency Injection is a software design pattern in which an object...]]></description>
										<content:encoded><![CDATA[<div class="TextHeading">
<div class="hddn">
<h2 id="dependency-injection-in-angularjs" class="color-green" style="text-align: justify;">Dependency injection in AngularJS</h2>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<ul>
<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>
</ul>
</div>
</div>
<div class="TextHeading" style="text-align: justify;">
<div class="hddn">
<h2 id="why-do-you-need-to-inject-dependencies" class="color-green">Why do you need to inject dependencies ?</h2>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<ul>
<li>Separate the process of creation and consumption of dependencies.</li>
<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>
<li>Allow concurrent/independent development of the dependency and the dependent entity, while only maintaining a known contract.</li>
<li>Facilitate changing of the dependencies when needed.</li>
<li>Allow injecting mock objects as dependencies for testing, by maintaining the agreed contract.</li>
</ul>
</div>
</div>
<div class="text-center row" style="text-align: justify;">
<div class="col-sm-12">
<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>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<ul>
<li style="list-style-type: none;">
<ul>
<li>The component can create the dependency, typically using the new operator.</li>
<li>The component can look up the dependency, by referring to a global variable.</li>
<li>The component can have the dependency passed to it where it is needed.</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<ul>
<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>
<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>
</ul>
</div>
</div>
<div class="subheading" style="text-align: justify;">
<h2 id="example-1">Example 1</h2>
<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>
</div>
<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>
<div class="ImageContent" style="text-align: justify;">
<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>
</div>
<p style="text-align: justify;" align="center">
<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>
<div style="text-align: justify;">
<div id="bsa-zone_1590522394795-6_123456"></div>
</div>
<div class="TextHeading" style="text-align: justify;">
<div class="hddn">
<h2 id="using-the-injector-service" class="color-green">Using the injector service:</h2>
</div>
</div>
<div class="subheading" style="text-align: justify;">
<h2 id="syntax">Syntax :</h2>
</div>
<div class="CodeContent" style="text-align: justify;">
<div class="hddn"></div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<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>
<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>
</div>
</div>
<div class="subheading" style="text-align: justify;">
<h2 id="example-2">Example 2</h2>
</div>
<div class="CodeContent" style="text-align: justify;">
<div class="hddn">
<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>
</div>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.wikitechy.com/interview-questions/angularjs/what-is-dependency-injection-in-angularjs/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
