12 Essential AngularJS Interview Questions *

Toptal sourced essential questions that the best AngularJS developers and engineers can answer. Driven from our community, we encourage experts to submit questions and offer feedback.

Hire a Top AngularJS Developer Now
Toptal logois an exclusive network of the top freelance software developers, designers, finance experts, product managers, and project managers in the world. Top companies hire Toptal freelancers for their most important projects.

Interview Questions

1.

List at least three ways to communicate between modules of your application using core AngularJS functionality.

View answer

Common ways to communicate between modules of your application using core AngularJS functionality include:

  • Using services
  • Using events
  • By assigning models on $rootScope
  • Directly between controllers, using $parent, $$childHead, $$nextSibling, etc.
  • Directly between controllers, using ControllerAs, or other forms of inheritance
2.

Which means of communication between modules of your application are easily testable?

View answer

Using a service is definitely easy to test. Services are injected, and in a test either a real service can be used or it can be mocked.

Events can be tested. In unit testing controllers, they usually are instantiated. For testing events on $rootScope, it must be injected into the test.

Testing $rootScope against the existence of some arbitrary models is testable, but sharing data through $rootScope is not considered a good practice.

For testing direct communication between controllers, the expected results should probably be mocked. Otherwise, controllers would need to be manually instantiated to have the right context.

3.

The most popular e2e testing tool for AngularJS is Protractor. There are also others which rely on similar mechanisms. Describe how e2e testing of AngularJS applications work.

View answer

The e2e tests are executed against a running app, that is a fully initialized system. They most often spawn a browser instance and involve the actual input of commands through the user interface. The written code is evaluated by an automation program, such as a Selenium server (webdriver). That program sends commands to a browser instance, then evaluates the visible results and reports back to the user.

The assertions are handled by another library, for Protractor the default is Jasmine. Before Protractor, there was a module called Angular Scenarios, which usually was executed through Karma, and is now deprecated. Should you want to e2e test hybrid apps, you could use another Selenium server, called Appium.

Testing can be handled manually, or it can be delegated to continuous integration servers, either custom or ones provided by Travis, SauceLabs, and Codeship.

Apply to Join Toptal's Development Network

and enjoy reliable, steady, remote Freelance AngularJS Developer Jobs

Apply as a Freelancer
4.

This is a simple test written for Protractor, a slightly modified example from Protractor docs:

it('should find an element by text input model', function() {
  browser.get('/some-url');

  var login = element(by.model('username'));
  login.clear();
  login.sendKeys('Jane Doe');
  var name = element(by.binding('username'));
  expect(name.getText()).toEqual('Jane Doe');

  // Point A
});

Explain if the code is synchronous or asynchronous and how it works.

View answer

The code is asynchronous, although it is written in a synchronous manner. What happens under the hood is that all those functions return promises on the control flow. There is even direct access, using “protractor.promise.controlFlow()”, and the two methods of the returned object, “.execute()” and “.await()”.

Other webdriver libraries, such as wd https://github.com/admc/wd, require the direct use of callbacks or promise chains.

5.

When a scope is terminated, two similar “destroy” events are fired. What are they used for, and why are there two?

View answer

The first one is an AngularJS event, “$destroy”, and the second one is a jqLite / jQuery event “$destroy”. The first one can be used by AngularJS scopes where they are accessible, such as in controllers or link functions.

Consider the two below happening in a directive’s postLink function. The AngularJS event:

scope.$on(‘$destroy’, function () {
  // handle the destroy, i.e. clean up.
});

And

element.on(‘$destroy’, function () {
  // respectful jQuery plugins already have this handler.
  // angular.element(document.body).off(‘someCustomEvent’);
});

The jqLite / jQuery event is called whenever a node is removed, which may just happen without scope teardown.

6.

How do you reset a “$timeout”, and disable a “$watch()”?

View answer

The key to both is assigning the result of the function to a variable.

To cleanup the timeout, just “.cancel()” it:

var customTimeout = $timeout(function () {
  // arbitrary code
}, 55);

$timeout.cancel(customTimeout);

The same applies to “$interval()”.

To disable a watch, just call it.

// .$watch() returns a deregistration function that we store to a variable
var deregisterWatchFn = $rootScope.$watch(‘someGloballyAvailableProperty’, function (newVal) {
  if (newVal) {
    // we invoke that deregistration function, to disable the watch
    deregisterWatchFn();
    ...
  }
});
7.

Name and describe the phases of a directive definition function execution, or describe how directives are instantiated.

View answer

The flow is as follows:

First, the “$compile()” function is executed which returns two link functions, preLink and postLink. That function is executed for every directive, starting from parent, then child, then grandchild.

Secondly, two functions are executed for every directive: the controller and the prelink function. The order of execution again starts with the parent element, then child, then grandchild, etc.

The last function postLink is executed in the inverse order. That is, it is first executed for grandchild, then child, then parent.

A great explanation of how directives are handled in AngularJS is available in the AngularJS Tutorial: Demystifying Custom Directives post on the Toptal blog.

8.

How does interpolation, e.g. “{{ someModel }}”, actually work?

View answer

It relies on $interpolation, a service which is called by the compiler. It evaluates text and markup which may contain AngularJS expressions. For every interpolated expression, a “watch()” is set. $interpolation returns a function, which has a single argument, “context”. By calling that function and providing a scope as context, the expressions are “$parse()”d against that scope.

9.

How does the digest phase work?

View answer

In a nutshell, on every digest cycle all scope models are compared against their previous values. That is dirty checking. If change is detected, the watches set on that model are fired. Then another digest cycle executes, and so on until all models are stable.

It is probably important to mention that there is no “.$digest()” polling. That means that every time it is being called deliberately. As long as core directives are used, we don’t need to worry, but when external code changes models the digest cycle needs to be called manually. Usually to do that, “.$apply()” or similar is used, and not “.$digest()” directly.

10.

List a few ways to improve performance in an AngularJS app.

View answer

The two officially recommended methods for production are disabling debug data and enabling strict DI mode.

The first one can be enabled through the $compileProvider:

myApp.config(function ($compileProvider) {
  $compileProvider.debugInfoEnabled(false);
});

That tweak disables appending scope to elements, making scopes inaccessible from the console. The second one can be set as a directive:

<html ng-app=“myApp” ng-strict-di>

The performance gain lies in the fact that the injected modules are annotated explicitly, hence they don’t need to be discovered dynamically.

You don’t need to annotate yourself, just use some automated build tool and library for that.

Two other popular ways are:

  • Using one-time binding where possible. Those bindings are set, e.g. in “{{ ::someModel }}” interpolations by prefixing the model with two colons. In such a case, no watch is set and the model is ignored during digest.
  • Making $httpProvider use applyAsync:
myApp.config(function ($httpProvider) {
  $httpProvider.useApplyAsync(true);
});

… which executes nearby digest calls just once, using a zero timeout.

11.

What is $rootScope and how does it relate to $scope?

View answer

$rootScope is the parent object of all $scope Angular objects created in a web page.

12.

What are the DOM and the BOM?

View answer

The DOM is the Document Object Model. It’s the view part of the UI. Whatever we are changing in page elements is reflected in the DOM.

BOM is the Browser Object Model, which specificies the global browser objects like window, localstorage, and console.

There is more to interviewing than tricky technical questions, so these are intended merely as a guide. Not every “A” candidate worth hiring will be able to answer them all, nor does answering them all guarantee an “A” candidate. At the end of the day, hiring remains an art, a science — and a lot of work.

Why Toptal

Tired of interviewing candidates? Not sure what to ask to get you a top hire?

Let Toptal find the best people for you.

Hire a Top AngularJS Developer Now

Our Exclusive Network of AngularJS Developers

Looking to land a job as an AngularJS Developer?

Let Toptal find the right job for you.

Apply as an AngularJS Developer

Job Opportunities From Our Network

Submit an interview question

Submitted questions and answers are subject to review and editing, and may or may not be selected for posting, at the sole discretion of Toptal, LLC.

* All fields are required

Looking for AngularJS Developers?

Looking for AngularJS Developers? Check out Toptal’s AngularJS developers.

Jelena Drobnjakovic

Freelance AngularJS Developer
United StatesToptal Member Since December 7, 2015

Jelena has been actively working as a front-end developer for about eight years. Her job includes daily usage of HTML, CSS, JavaScript, Angular/Vue, jQuery, Laravel, and Git. She loves creating beautiful web pages that are optimized and working smoothly on all devices. Besides programming, she has also overseen the organization of work for some projects, which she loves doing. Communication between colleagues is key to building good applications.

Show More

Muhammed Mutahr

Freelance AngularJS Developer
United StatesToptal Member Since June 9, 2018

Muhammad is a senior software engineer with several years of experience in industries ranging from the public education sector (WSU), privately owned healthcare companies (Meridian), and public Fortune 500 companies in the automotive industry (GM & AAM). Throughout his career, he’s designed web apps in HTML/CSS, developed client-side apps using JavaScript frameworks (Angular/Ionic), and used Java and C# to develop robust server-side apps.

Show More

Thien Nguyen

Freelance AngularJS Developer
GermanyToptal Member Since October 6, 2020

Thien is a professional software engineer with a focus on JavaScript and front-end technologies. Thien has more than 15 years of experience developing web applications, websites, and games using various technologies and frameworks including Angular, Angular Material, React (and other frameworks), Express, TypeScript, among others. Due to his in-depth experience covering various processes, project types, and platforms, you can trust Thien to deliver.

Show More

Toptal Connects the Top 3% of Freelance Talent All Over The World.

Join the Toptal community.

Learn more