Angularjs Interview Questions and Answers Set 8

71.Mention the steps for the compilation process of HTML happens?

Compilation of HTML process occurs in following ways:-

=> Using the standard browser API, first the HTML is parsed into DOM

=> By using the call to the $compile () method, compilation of the DOM is performed. The method traverses the DOM and matches the directives.

=> Link the template with scope by calling the linking function returned from the previous step.

72.Explain what is DI (Dependency Injection ) and how an object or function can get a hold of its dependencies?

DI or Dependency Injection is a software design pattern that deals with how code gets hold of its dependencies. In order to retrieve elements of the application which is required to be configured when module gets loaded , the operation “config” uses dependency injection.

These are the ways that object uses to hold of its dependencies:

Typically using the new operator, dependency can be created

By referring to a global variable, dependency can be looked up

Dependency can be passed into where it is required

73.Explain the concept of scope hierarchy? How many scope can an application have?

Each angular application consist of one root scope but may have several child scopes. As child controllers and some directives create new child scopes, application can have multiple scopes. When new scopes are formed or created they are added as a children of their parent scope. Similar to DOM, they also creates a hierarchical structure.

74.Explain what is Dependency Injection in AngularJS?

Dependency Injection is one of the best features of AngularJS. It is a software design pattern in which objects are passed as dependencies. It helps us to remove hard coded dependencies and makes dependencies configurable. Using Dependency Injection, we can make components maintainable, reusable and testable.

75.Dependency Injection is required for the following

Separating the process of creation and consumption of dependencies.

It allows us to create independent development of the dependencies.

We can change the dependencies when required.

It allows injecting mock objects as dependencies for testing.

AngularJS uses dependency with several types

Value

Factory

Service

Provider

Constant

A simple case of dependency injection in Angular js

AppModule.controller(“AppController”, function($scope, $window, $log,$http)   {   });

76.What Is Singleton Pattern? How Does Angular Use It?

A singleton pattern is an approach that we adopt to limit the instantiation of a Class to have only one object. In Angular, the dependency injection and the services implement the singleton pattern.

Technically, if we call the “new Class()” two times without following the singleton pattern, the outcome will be two objects of the same class. Whereas a singleton enabled class will create the object first time and return the same object onwards.

77.Explain what is factory method in angular.js?

For creating the directive, factory method is used. It is invoked only once, when compiler matches the directive for the first time. By using $injector.invoke the factory method is invoked.

78.When To Use Factory?

It is just a collection of functions, like a class. Hence, it can be instantiated in different controllers when you are using it with a constructor function.

79.Explain what is services in Angular.js ?

In angular.js services are the singleton objects or functions that are used for carrying out specific tasks. It holds some business logic and these function can be called as controllers, directive, filters and so on.

80.What Does A Service Mean In AngularJS? Explain Its Built-In Services?

Services are functions that are bound to perform specific tasks in an application.

It gives us a method, that helps in maintaining the angular app data for its lifetime.

It gives us methods, that facilitate to transfer data across the controllers in a consistent way.

It is a singleton object and its instance is created only once per application.

It is used to organize and share, data and function across the application.

Two main execution characteristics of angular services are that they are Singleton and lazy instantiated.

  1. Lazily instantiated –

It means that AngularJS instantiates a service only when a component of an application needs it. This is done by using dependency injection method, that makes the Angular codes, robust and less error prone.

  1. Singletons –

Each application component dependent on the service, work with the single instance of the service created by the AngularJS.

Let us take an example of a very simple service that calculates the square of a given number:

var CalculationService = angular.module(‘CalculationService’, []).service(‘Calculation’, function () {    this.square = function (a) { return a*a};});

AngularJS Internal Services –

AngularJS provides many built-in services. Each of them is responsible for a specific task. Built-in services are always prefixed with the $ symbol.

Some of the commonly used services in any AngularJS application are as follows:

$http – used to make an Ajax call to get the server data.

$window – Provides a reference to a DOM object.

$Location – Provides reference to the browser location.

$timeout – Provides a reference to window.set timeout function.

$Log – used for logging.

$sanitize – Used to avoid script injections and display raw HTML in the page.

$Rootscope – Used for scope hierarchy manipulation.

$Route – Used to display browser based path in browser URL.

$Filter – Used for providing filter access.

$resource – Used to work with Restful API.

$document – Used to access the window. Document object.

$exceptionHandler – Used for handling exceptions.

$q – Provides a promise object.

$cookies – This service is useful to write, read and delete browser cookies.

$parse – This service is useful to convert AngularJS expression into a function.

$cacheFactory – This service evaluates the specified expression when the user changes the input.