AngularJS Basic Interview Questions

Angularjs basic interview questions and answers

Find basic top 30 Angular js interview questions and their answers

#1 What is the difference between ng-if, ng-show/ng-hide?

Based on the expression’s boolean result, the ng-if directive removes or recreates the element in the dom. If the expression resolves to true it removes the element from the dom otherwise a clone of the element gets inserted.

Based on the expression’s boolean result,ng-show directive hides or shows the element in the dom. If the expression resolves to false it hides the element by adding ng-hide css class on the element otherwise the class is removed from the element.

#2 What is a Single page application or SPA?

Single page applications or SPA’s are web applications in which the html page gets loaded once and then it gets dynamically updated as the user engages with the web application. The purpose of SPA is to provide a more native app like experience to the user being a web application. Ajax plays an important role, as in SPA’s we do not load the entire page for web components say for example html templates, instead make an ajax call and get the templates needed. The big advantage of a SPA is it’s ability to update UI without doing a server round trip, this reduces a lot of time and server load.

#3 What are the symbols used in one way and two ways data binding?

Two-way data binding is a feature provided by the angular framework using which the view and model of an MVC application are in sync all the time i.e. change in view updates model and vice versa. To achieve two-way data binding “ng-model” directive is used. By default, ng-model provides the two-way data model functionality. In order to achieve one-way data binding, ng-model can be used in below fashion: ng-model=”::object.value”

#4 Explain the need for DI in AngularJs?

Dependency Injection or DI, is a design pattern of injecting objects into the angular components instead of angular components creating for themselves. The advantage of this is, we can separate our web application in multiple logical units, we can reuse these multiple logical units anywhere in our application. In angular dependency injection is the main concept without which angular does not work. For example, In order to use services(built-in or custom or both) we have to inject these services in our controller as shown below.


angular.controller('myCntrl',function($scope,$http)
{
 //Use your $http service here 

});

#5 Can digest cycle be forced to run manually?

Digest cycle can be forced to run by using $apply() method.

Read 80+ AngularJS Interview s for beginners.

#6 What is a module?

A module is a block which defines the boundaries of an angular application.Outside a module ,the angular features and properties will not work. It is implemented by using the directive “ng-app” and is referenced just below the angular framework’s javascript files. It is defined in the following fashion : 

 var app = angular.module("myApp", []);

In this code snippet,”myApp” is the name of the module and is mandatory.The square brackets “[]”, help to inject dependencies in our angular application.

#7 What is an isolated scope?

In Angularjs, directives by default inherit all the properties from the parent scope, and while creating a custom directive one may need some parent scope properties. There is a possibility that the parent scope at some point in the application lifecycle may get removed/deleted, on the removal of that property the custom directive becomes completely useless.

So to make a custom directive that can access the parent scope properties and at the same time does not rely on it we use isolated scope.

#8 What is a service? How will you create a custom service

The separation of concerns in AngularJS is achieved by using a service. A Service is a singleton javascript object and gets instantiated only once during an applications lifetime. Services provide some very useful methods essential to implement some powerful angular concepts.

For example $http service provides methods like GET, POST, PUT and DELETE which help to contact the server and help perform CRUD operations. Angular also provides the ability to create custom services. A service can be created in the below fashion in an angular application :


angular.module('myModule', []).factory('serviceId', function() {
var shinyNewServiceInstance;
return shinyNewServiceInstance;
});

#9 What is AngularJS?

AngularJS is a javascript framework which helps in developing web applications in MVC architectural pattern & SPA or Single page applications. It simplifies binding of the business logic with a view.It’s an open source framework but is supported by Google.

#10 What are angular directives?

Angular directives are the attributes which are decorated on the html tags or elements of an html page. Directives have a prefix “ng-” and are used for dom manipulations & data binding. Some of the most common directives are : ng-app, ng-controller,ng-repeat,ng-if etc.

#11 How can a number of watchers and digest cycle time affect the performance of the application?

The working of digest cycle involves going to each and every watcher and informing it about the model change. This task will consume more time if the number of watchers will increase. Angular team has recommended implementing not more than 2000 watchers per page. To check the performance implications of digest cycle, we can use online tools like Batarang. These tools are mere extensions of google chrome & show the time taken by each digest cycle on a single page.

#12 What is $rootscope?

$rootscope is the object instance of the module, it is the parent of all the scopes in an angular application. It acts as a global variable and can be accessed by any component of the web application.

#13 What is the use of restrict and template property when creating custom directives?

Custom directives gives us the ability to create our own directives. Its restriction property helps us to enforce the decision of how this custom directive should be used. The restriction property takes four values as input:

  • E- If E is the value for the restriction property, the directive can only be used as an element in an html page,
  • A- If A is used as a value, the directive can be used as an attribute in any html tag,
  • C-If C is used, the directive gets triggered if same CSS class is used,
  • M-If M is used, the directive gets triggered if same comment is used.

These four values can also be combined and used, for example : ‘EA’ if provided as a value will allow the directive to be used as an element as well as an attribute. ‘EAC’ can also be set as a value for restrict. “template” property is used in a directive if you have to inject a template in your html page.”template” property takes an html template as an input. The main objective of this property is to reduce the re-work involved in writing same code again and again.

#14 Explain Digest cycle, watchers and dirty checking?

AngularJS is an MVC framework and one of it’s most powerful feature is two-way data binding, which means that model and view are in sync all the time. If there is a change in the model value, the view gets updated automatically. Angular achieves this by running a loop known as digest cycle. Digest cycle goes through the following steps to update the view if a model has changed or vice versa :

  • Step 1: Whenever an end user triggers some kind of event like typing, button click etc, such an activity leads to the change of model data.
  • Step 2: Angular framework then checks whether the old value and the new value are same. If the values have not changed, it does nothing but if the values have changed then Digest cycle gets invoked.
  • Step 3: Digest cycle then runs through all the scope objects of the controller and checks whether any scope object has been affected due to the model change. Every scope object has watchers, these watchers listen whether the model has changed or not.Digest cycle informs these watchers about model change and then finally these watchers synchronize the view with the model data.
  • Step 4: When watchers synchronize the view with the model data, there is a possibility that model may get updated again. To make sure that this change in model gets synchronized with the view, angular runs the digest cycle once again. This second loop of digest cycle is termed as “Dirty Checking”.

#15 How will you transfer data between two controllers?

The most optimal way of transferring data between two controllers is by using a custom service. We achieve this result by creating a custom service with properties or functions which can be accessed by both the controllers.

Latest Posts.

Angularjs Tutorials
AngularJS: A better Option to grow your career

As you may wondering to make your future dependable on AngularJS, here we tell you what an AngularJS...

Angularjs Tutorials
6 AngularJS Frameworks to learn

The AngularJS, one of the most prominent frameworks, has rightly been identified as the leading open...

Angularjs Tutorials
Top 10 key features of Angular 5

Angular JS has been in the industry for quite a few years with micro updates. However, the release o...

Ask a Question