10 Most Important AngularJS Interview Questions
These are the 10 trickiest interview questions that are asked in AngularJS Interviews.
1: How to share data between two controllers?
The easiest and efficient way to do this is to create an angular service that will hold the data and then inject those services inside angular controller functions. This will share the same data to two different controllers in an easy way.
However, you can achieve this functionality by other means:
like using $parent, nextSibling, controllerAs, etc. to access the controllers directly.
Using $events we can achieve this.
$rootScope to include data globally achieves the requirement but it’s not recommended as it consumes a lot of data and also a security breach to the application.
you can check the video for the detailed explanation on this.
2: What is the difference between ng-if and ng-show/ng-hide directives?
ng-if will not insert the DOM element until unless specified condition is satisfied.
ng-show or ng-hide will first insert the DOM element and then shows or hides depending on the condition result
ng-show or ng-hide makes the page bit slower as the DOM elements are added prior to condition check which increases the DOM structure whereas, using ng-if page loads faster as the DOM element is loaded only when the condition is satisfied.
3: Can you explain about digest cycle in angular?
In one complete Digest cycle, angular compares the old value with the newest updated values of the scope model and reflects those changes to view automatically.
By default, Digest cycle is triggered automatically. If We want we can set it manually by calling $apply() but it’s not recommended.
4: What’s the best place for DOM manipulation in AngularJS application?
The best place for including DOM manipulation in angularJS is in its directives. It’s not the best approach to include DOM manipulations in Controllers, services or anywhere else except directives. The reason for this is directives are the reusable, independent and testable piece of code.
5: How can you differentiate One-way and two-way binding in AngularJS?
One-way binding means the scope variable remains same to the value it has been set initially. Whereas, in Two-way binding scope variables changes its value every time its Model is assigned to a different value.
6: What directive will you use to hide elements from HTML DOM by removing from the DOM but not affecting their styling?
We can use ngIf Directive to the element to remove that element from the DOM if the condition evaluates to false.
7: What is the significance of angular.copy() method in AngularJS?
angular.copy() creates the deep copy of a variable.
Deep copy of a variable does not point to the same reference as the old copy of the variable. Usually, if we assign one variable to another variable it creates a shallow copy that is if one variable changes other will also change.
8: What is $scope in angularJS?
A scope is an object that refers to the application model. It provides an execution context for expressions.Scope variables are arranged in a hierarchical structure similar to DOM structure of an application.Scopes are objects that refer to model. They can watch expressions and propagate events accordingly. They act as a mediator between controller and view.
9: What is Singleton design pattern, what’s its importance in AngularJS?
Singleton Design pattern restricts the use of class more than once. We can find singleton pattern behavior in angular controllers and services.
For instance: If you create the same object two times by using the new keyword you will be reusing already created one. Without singleton pattern you world be getting two pieces of memory for the same object.
10: How will you validate text input field for a login form in AngularJS?
Using ngPattern directive we can perform text validations of model attributes. This basically checks for an input text validation according to the given regular expression in the ngPattern directive.
By using this functionality you can easily validate names, phone number, email, password check and zip codes etc very easily.