AngularJS Interview Questions – Set 4

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 Set

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:

  • Implicitly from the function parameter names such as that demonstrated in following example. The function parameter names are the names of dependencies.
    function MyController($scope, greeter) {
      // ...
    }
  • Using the $inject property annotation. In the example below, the ordering of the values in the $inject array must match the ordering of the arguments to inject.
    var MyController = function(renamed$scope, renamedGreeter) {
      ...
    }
    MyController['$inject'] = ['$scope', 'greeter'];
  • Using the inline array annotation. This is the recommended approach. In below, an array is passed to the controller function. In this array, elements consists of a list of strings (such as ‘$scope’, ‘greeter’ ) followed by function.
    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.


Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

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() { ... });

Recent Posts

Agentic Reasoning Design Patterns in AI: Examples

In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…

1 month ago

LLMs for Adaptive Learning & Personalized Education

Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…

2 months ago

Sparse Mixture of Experts (MoE) Models: Examples

With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…

2 months ago

Anxiety Disorder Detection & Machine Learning Techniques

Anxiety is a common mental health condition that affects millions of people around the world.…

2 months ago

Confounder Features & Machine Learning Models: Examples

In machine learning, confounder features or variables can significantly affect the accuracy and validity of…

2 months ago

Credit Card Fraud Detection & Machine Learning

Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…

2 months ago