The article represents another set of 10 interview questions primarily related with dependency injection. Following are another set of questions that have already been published earlier.
Question:Describe what happens when Angular compiler comes across “ng-controller” directive?
Ans: As the Angular compiles the HTML and come across the “ng-controller” directive (e.g.,<body ng-controller=”HelloCtrl”>), it asks the injector to create an instance of controller and its dependencies. Injector then looks out for any mechanism that has been specified by the user for creating the controller. In order to specify how the controller (HelloCtrl in current example) needs to be instantiated, we define the controller in one of the following manner:
function HelloCtrl($scope) { ...the controller code goes here... }
Following is another (recommended) way to instantiate the controller:
var helloApp = angular.module( "helloApp", [] ); helloApp.controller( "HelloCtrl", ['$scope', function( $scope ){ ...the controller code goes here... }]);
Thus, the injector uses one of the above mechanism to instantiate the controller.
Question:Describe what happens when Angular compiler comes across “ng-app” directive?
Ans: As Angular compiler comes across the “ng-app” directive, it looks out for the mechanism used by the user to instantiate the module. If not found, it instantiates the app with default module, “ng”. In case, it finds the following instantiation mechanism, it uses below to intantiate the application. Note that following is preferred way of loading an app along with other module dependencies.
var helloApp = angular.module( "helloApp", [] );
Question:What are various different ways of annotating the services/objects that injector uses to resolve the dependency?
Ans: Following are three different ways for annotating the code with the services information:
function MyController($scope, greeter) { // ... }
var MyController = function(renamed$scope, renamedGreeter) { ... } MyController['$inject'] = ['$scope', 'greeter'];
helloApp.controller( "HelloCtrl", ['$scope', 'greeter', function( $scope, greeter ) { ... }]);
Question:Write the code representing Controller construction and representing dependency injection using inline array annotation?
Ans: Following code represents defining controller constructor while providing annotations for dependency services (depService in example below) using inline array:
helloApp.controller( "HelloCtrl", ['$scope', 'depService', function( $scope, depService ) { ... }]);
Question:Write the code representing service construction recipe and representing dependency injection using inline array annotation?
Ans: Following code represents defining service constructor while providing annotations for dependency services (depService in example below) using inline array:
helloApp.factory( "serviceName", [ 'depService', function( depService ) { ... }]);
Question:Write the code representing factory method for creating a custom directive while using inline array annotation for dependency injection?
Ans: Following code represents creation of custom directive while providing annotations for dependency services (depService) using inline array:
helloApp.directive('directiveName', ['depService', function(depService) { ... }])
Question:Write code representing run method that accepts a function which can be injected with services/values/constants components?
Ans: The run method accepts a function, which can be injected with “service”, “value” and “constant” components as dependencies. Following is the code sample assuming helloApp is the app name:
var helloApp = angular.module( "helloApp", [] ); helloApp.run(['depService', function(depService){ ... }])
Question: Write code representing config method that accepts a function which can be injected with provider and constant components?
Ans: The config method accepts a function, which can be injected with “provider” and “constant” components as dependencies. Following is the code sample assuming helloApp is the app name:
var helloApp = angular.module( "helloApp", [] ); helloApp.config(['depProvider', function(depProvider){ ... }])
Question:Which components type can not be injected into “run” method?
Ans: “provider” types can not be injected into “run” method.
Question:Which components type can not be injected into “config” method?
Ans: “service” and “value” component types can not be injected into “config” method.
Question:Can ‘$scope’ be injected while creating service using “factory” method?
Ans: No.
Feel free to suggest/comment on above interview questions. Watch out this space for more questions’ sets.
Retrieval-Augmented Generation (RAG) is an innovative generative AI method that combines retrieval-based search with large…
The combination of Retrieval-Augmented Generation (RAG) and powerful language models enables the development of sophisticated…
Have you ever wondered how to use OpenAI APIs to create custom chatbots? With advancements…
When building a Retrieval-Augmented Generation (RAG) application powered by Large Language Models (LLMs), which combine…
Last updated: 25th Jan, 2025 Have you ever wondered how to seamlessly integrate the vast…
Artificial Intelligence (AI) agents have started becoming an integral part of our lives. Imagine asking…
View Comments
Great set of questions. Maybe in your last example:
var helloApp = angular.module( "helloApp", [] );
helloApp.config(['depProvider', function(depProvider){
...
}])
It may worth mentioning that the injected service's name is 'dep':
angular.module("helloApp").provider('dep', function() { ... });
Thanks Jose for your comment.