1. What is AngularJS?

Ans. AngularJS is a client-side JavaScript Model-View-Controller (MVC) framework used for developing rich and extensible web applications. It mainly runs on plain JavaScript and HTML and is suitable for Single Page Applications (SPA).

  1. What are the different features of AngularJS?

Ans. The various features of AngularJS are:

  • Architecture
  • Codeless
  • Data Binding
  • Deep Linking
  • Dependency Injection
  • Directives
  • Not Browser Specific
  • Productivity
  • Routing
  • Speed and Performance
  1. What are Directives in AngularJS?

Ans. AngularJS lets you extend HTML with new attributes called Directives. Thus, Directives in AngularJS are extended HTML attributes. Most of the directives in AngularJS come with the prefix ng-.  Some of the built-in Directives are:

  • ng-app – initializes AngularJS and makes the specified element a root element of the application
  • ng-init – initializes variables in AngularJS application
  • ng-bind – binds the model property declared via $scope or ng-model directive, or the result of an expression to the HTML element
  • ng-model – binds <input>, <select> or <textarea> elements to a specified property on the $scope object
  • ng-repeat – repeats HTML once per each item in the specified array collection
  • ng-app – it initializes an AngularJS application
  1. What are Expressions in AngularJS?

Ans. Expressions in AnjularJS are used to bind application data to HTML. They are written inside double curly braces like {{expression}} and behave in the same way as ng-bind directives. They are pure JavaScript expressions and output the data where they are used. 

For example: <p>Name: {{name.title}}</p>

  1. Explain $scope in Angular?

Ans. ‘$scope’ is a built-in object containing application data and methods. ‘$scope’ object instance gets created when ‘ng-controller’ directive is encountered. 

For example:

function Function1($scope)

$scope.ControllerName = “Function1”;

function Function2($scope)

$scope.ControllerName = “Function2”;

  1. What should be the maximum number of concurrent “watches”?

Ans. There should be a maximum of 2000 to reduce memory consumption and improve performance.

  1. Can we force the digest cycle to run manually?

Ans. Yes, we can force the digest cycle to run manually by using $apply().

  1. Which is the latest version of angular?

Ans. Angular 8 is the latest version of angular.

  1. What is Data Binding in AngularJS?

Ans. The automatic synchronization of data between the model and view components is referred to as Data Binding in AngularJS. AngularJS applications usually have a data model, a collection of data available for the application. The View is the HTML container where the AngularJS application is displayed.

  1. What are the different types of Directives?

Ans. The different types of Directives are –

  • Components directives
  • Structural directives
  • Attribute directives 
  1. Name different types of Linking Function.

Ans. The two types of Linking Functions are pre-linking function and post-linking functions.

  1. What is the Controller in AngularJS?

Ans. A Controller is a set of JavaScript functions that are bound to a specified scope, the ng-Controller directive.

  1. Is AngularJS compatible with all browsers?

Ans. Yes, it is compatible with all browsers, including Google Chrome, Mozilla Firefox, Safari, Opera, Internet Explorer, and Mobile browsers.

  1. Name some major browsers supported by AngularJS?

Ans. The following are some of the major browsers supported by AngularJS:

  • Google Chrome
  • Mozilla Firefox
  • Microsft Edge
  • IE 10, 11, 
  • IE Mobile
  • Safari
  1. Name the building blocks of the Angular.

Ans. Following are the building blocks of Angular:

  • Blocks
  • Lane
  • Template
  • Component
  • Services
  • Pro injection
  • Orders
  • Data Nemo
  1. What is the difference between Angular and AngularJS?

Ans. The differences between Angular and AngularJS are: 

Feature

AngularJS

Angular

Components

AngularJS is based on JavaScript.

Angular uses a superset of ES6, which has backward compatibility with ES5.

Architecture

It uses MVC (Model View Controller) design model. ‘View’ displays the information present in the model and ‘Controller’ processes the information.

It uses components and directives.

Language

Code is written in Javascript

Code is written in TypeScript

Expression Syntax

ng-bind is used to bind data from view to model.

() is used to bind an event and [] for property binding.

Mobile Support

AngularJS code is not mobile-friendly.

It is supported by all the popular mobile browsers.

Routing

It uses $routeprovider.when() for routing configuration.

Angular uses @RouteConfig{(…)} is used for routing configuration.

Dependency Injection

It does not use Dependency Injection.

It uses a Hierarchical Dependency Injection system.

  1. Explain dirty checking. 

Ans. Dirty checking in Angular is implemented for two-way data binding on $scope variables. The Digest cycle tracks the watchlist and checks if there any changes in the value of the watch variable. It compares the new scope model values with the previous scope values. In case of any changes in the value of any variable, it forces to reassign values of other watched variables in DOM. Watchers will update the view as per the model value change. The Digest cycle will run again to check that all the values are synced up. This is termed Dirty Checking.

  1. How will you update Angular 6 to Angular 7?

Ans. We can update Angular 6 to Angular 7 by using the following command:

ng update @angular/cli @angular/core  

  1. What is a Digest Cycle in AngularJS? How does it work?

Ans. AngularJS Digest Cycle refers to the process of data binding. It observes the watchlist and keeps a track of changes in the value of the watch variable. In each digest cycle, Angular compares the previous and the new version of the scope model values. This process is triggered automatically but we can also trigger it manually by using $apply().

  1. How can you decrease Digest Cycle Time?

Ans. When a digest cycle is triggered, it loops over every binding to detect model changes. The digest cycle time can be decreased by reducing the number of watchers. This also reduces application memory footprints.

  1. How can you share data between Controllers?

Ans. There are two main scenarios while sharing data between Controllers:

  1. Sharing data between a parent and a child controller
  2. Sharing data between unrelated controllers

The sharing of data between a parent and a child controller can be done by: 

  • using $parent in HTML code
  • use of $parent in child controller
  • using controller inheritance

Sharing data between controllers without having relation – It can be done by:

  • Holding the shared data in a factory or service
  • Using the rootScope variable
  • Use of events to notify other controllers about changes to the data
  1. What is the Currency Filter? What are the two ways to use Currency Filters?

Ans. The Currency Filter formats a number to a currency format. If no currency format is specified, then the currency filter uses the local currency format. 

Currency Filter Syntax:

{{currencyexpression | currency : symbol : fractionsize}}

  1. What is Dependency Injection?

Ans. Dependency Injection is a process where we inject the dependent objects rather than the consumer creating the objects. It is a design pattern used to implement IoC. Dependency Injection is providing the objects that an object needs (its dependencies) instead of having it construct them itself. It involves 3 types of classes.

  • Client Class
  • Service Class
  • Injector Class
  1. What are the benefits of Dependency Injection?

Ans. The following are the benefits of Dependency Injection:

  • It offers a client the flexibility of being configurable
  • It allows testing to be performed using mock objects
  • Loosely coupled architecture
  1. What is the “$rootScope” in AngularJS?

Ans. Scope provides a separation between View and its Model. Every application has a $rootScope provided by AngularJS, and every other scope is its child scope. If a variable has the same name in both the current scope and in the rootScope, then the application uses the one in the current scope.

  1. How to create a service in AngularJS?

Ans. In AngularJS, the service is created by registering it with the module in which it will operate. It can be created in three ways: 

  • Factory
  • Service
  • Provider
  1. Mention the major differences between AngularJS and JavaScript Expressions.

Ans. Some of the significant differences between AngularJS and JavaScript Expressions are –

Angular Expressions

JavaScript Expressions

1. AngularJS expressions  are evaluated against a scopeobject

1. They are evaluated against the global window

2. AngularJS expressions can be written in HTML

2. JavaScript expressions cannot be written in HTML

3. These expressions don’t support conditionals, loops, and exceptions

3. JavaScript expressions support conditionals, loops, and exceptions

4. AngularJS expressions support filters

4. They do not support filters

5. One time binding is supported

5. One time binding is not supported

  1. What is the SPA (Single page application) in AngularJS?

Ans. SPAs are web applications that load a single HTML page and dynamically update that page as the user interacts with the app.

  1. Which directive is used to hide elements from the HTML DOM by removing them from that DOM and not changing their styling?

Ans. ngIf Directive is used to hide elements from the HTML DOM by removing them from that DOM and not changing their styling

  1. How do you disable a button depending on a checkbox’s state?

Ans. We can disable a button depending on a checkbox’s state by using the ng-disabled directive.

  1. What is Representational State Transfer (REST) in AngularJS?

Ans. REST is a style of API that operates over HTTP requests. The requested URL identifies the data to be operated on, and the HTTP method identifies the operation that is to be performed.

  1. When should you use an Attribute versus an Element?

Ans. An Attribute is used when we are decorating an existing element with new functionality.

An Element is used when we are creating a component that is in control of the template.

  1. Name some commonly used inbuilt services in AngularJs.

Ans. There are 30 inbuilt services in AngularJs. Some of the commonly used services include: 

  • $location
  • $scope
  • $http
  • $window
  • $interval
  • $timeout
  1. What is DDO (Directive Definition Object)?

Ans. DDO is an object used while creating a custom directive. It tells the compiler how a Directive needs to be assembled. Some of the properties include the link function, controller function, template, restrict, and templateUrl.

  1. What is the Provider Method in AngularJS?

Ans. A provider is an object that creates a service object by allowing it to take more control. It is an object with a $get() method. The $get() method used in the provider returns the service object. The service name and the factory function are the arguments that are passed into the provider method. AngularJS uses $provide to register new providers.

Syntax:

 serviceApp.provider(“logService”, function ()) 

  1. What are the attributes that can be used during the creation of a new AngularJS Directives?

Ans. Restrict, Template URL, Template, Replace, Transclude, Scope, Require, Controller, Link, and Compile are used during the creation of new AngularJS Directives.

  1. What are the Angular Modules?

Ans. The Angular Modules collectively define an angular application that enables one to write angular codes. It is a mechanism to group components, directives, pipes, and interrelated services. Angular Modules create an application by combining it with other modules.

  1. What are the directive scopes in AngularJs?

Ans. There are three directive scopes in AngularJs.

  • Parent scope – Any change made in the directive comes from the parent scope. It is also a default scope.
  • Child scope – This scope inherits a property from the parent scope.
  • Isolated scope – It is a reusable scope when a self-contained directive is built. It does not have any properties of the parent scope.
  1. What is the Template?

Ans. Templates are the static HTML part of the Angular app and comprise of attributes and directives. They have an additional syntax that ensures customized user experience through a data injection process.

  1. What is Internationalization?

Ans. Internationalization is used to create multilingual websites. It facilitates presenting locale-specific information on a website.

  1. What is the String Interpolation?

Ans. String Interpolation is a service to evaluate angular expressions by matching the text and attributes to see if they have any embedded expressions.

  1. What are AngularJS prefixes $ and $$?

Ans. These prefixes are used to avoid any accidental code collision with the user’s code.

$ prefix – Used with public objects

$$ prefix – Used with private objects

  1. What is Strict Contextual Escaping?

Ans. Strict Contextual Escaping is a mode in which AngularJS constraints untrusted binding values. Here the AngularJS automatically runs security checks or shows an error if it cannot guarantee the security of the result.

  1. What is the Factory Method?

Ans. Factory Method is a process of creating the directive, and it is invoked only when a compiler matches the directive for the first time. We can use $injector.invoke to invoke the factory method.

  1. What is Deep Linking?

Ans. Deep Linking enables encoding the application state in any URL such that it can be bookmarked, and the application can be restored.

  1. What is the role of $routeProvider in AngularJs?

Ans. $routeProvider helps to navigate between different pages or links without loading them separately. It is configured by using ngRoute config().

  1. What is ng-switch?

Ans. In AngularJs, ng-switch conditionally exchanges the structure of the Document Object Model (DOM) on a scope-based expression template. In simpler words, it is used to show/hide the child elements on the HTML page.

  1. What is the AngularJs Global API?

Ans. It is a set of global JavaScript functions such as protocols, routines, and tools for building software applications. Global API helps the users to perform tasks like compare and iterate objects, and convert data while interacting with the application.

  1. Name some common API functions.

Ans. Some of the common API functions are –

  • angular. Lowercase – Converts a string to lowercase string
  • angular. Uppercase – Converts a string to uppercase string
  • angular. isString – Returns true if the reference is a string
  • angular. isNumber – Returns true if the reference is a number
  1. What are events in AngularJS?

Ans. AngularJS Events are specific directives that can be used to customize the behavior of various DOM events like click, dblclick, mouseente. Some of the events supported by Angular are:

AngularJS Event Directives

ng-click

ng-blur

ng-click

ng-copy

ng-dblclick

ng-keydown

ng-keypress

ng-keyup

ng-mousedown

ng-mouseenter

ng-mouseleave

ng-mousemove

ng-mouseover

ng-mouseup

  1. How will you validate a URL?

Ans. A URL can be validated by adding the regex directly to the ng pattern to the attribute.

  1. What is Event Handling?

Ans. Event Handling in AngularJs is used to create advance AngularJs applications. When you create more advanced AngularJS applications, your application will need to handle DOM events like mouse clicks, moves, keyboard presses, etc. AngularJS has a simple model for how to add event listeners to the HTML you generate from your views.

  1. Name a few tools for testing AngularJS applications.
  1. Karma
  2. Angular Mocks
  3. Browserify
  4. Mocha
  5. Sion
  1. When do we use $location?

Ans. $location keeps track of the application URL and makes it available to a controller. $location can be used any time an application needs to react to a change in the existing URL or whenever we want to change the current URL in the browser.

  1. What is the role of Compile and Link in AngularJS?

Ans. Compile and Link perform the following functions: 

  • Compile – It traverses the HTML, finds for all the directives, and returns a link function.
  • Link – It combines the model with a view. In case of any change in the model, it is reflected in view, and in case of any change in view, it is reflected in the model.
  1. Explain sessionStorage, cookies, and localStorage.

Ans. SessionStorage – It stores data for a particular session, and data will be lost when a browser tab is closed. Maximum storage is up to 5MB.

Cookies – It stores data that has to be sent back to the server or every HTTP request. We can set expiration from either the server-side or client-side, varying on the type and duration. Maximum storage is up to 4KB.

LocalStorage – It stores with no expiration date, and data can only be cleared by JavaScript or by clearing the browser cache. The storage limit is more than both sessionStorage and cookies.

  1. Give an example to define a custom event.

Ans. Custom event can be defined using “objEvent = new EventEmitter()”

  1. Which method of RouterModule provides all routes in AppModule?

Ans. RouterModule.forRoot is used for providing all routes in AppModule.

  1. What type of data can be used with an async pipe?

Ans. Promise, Observable can be used with an async pipe.

  1. What are the ways that components follow to communicate with each other?

Ans. There are three ways to communicate with components:

  • Passing a component to another component
  • When the value is passed by using parent component
  • Passing through service
  1. What is the procedure to create two-way data binding in Angular?

Ans. Two-way data binding can be created by combining the input and output binding into a single notation using the ngModel directive.

For Example: <input [(ngModel)]=”name” >  {{name}}

  1. Explain One-Way Binding and Two-Way Binding.

Ans. One-Way Binding implies that the scope variable in the HTML will be set to the first value its model is bound to.

Two-Way Binding implies that the scope variable will change its value every time its model is assigned to a different value.

  1. What are the different Angular Life Cycle hooks?

Ans. Following are the Angular life cycle hooks:

  • ngAfterViewInit ()
  • ngOnInit ()
  • ngDoCheck ()
  • ngOnChanges ()
  • ngAfterViewChecked ()
  • ngOnDestroy ()
  • ngAfterContentInit ()
  • ngAfterContentChecked ()
  1. What are the different ways to create a decorator in Angular?

Ans. Following are two ways to create a decorator in Angular:

  • Module.decorator
  • $ provide.decorator
  1. Name the different types of NgModules.

Ans. There are four types of NgModules:

  • Service block
  • Widget block
  • Features module
  • Replacement module
  • Shared volume
  1. Mention the different types of built-in pipes.

Ans. Following are the different types of built-in pipes:

  • Titlecase
  • Uppercase
  • Lowercase
  • Percent
  • Slice
  • Currency
  • Date
  • Decimal
  • Json
  1. Explain bootstrapping in Angular.

Ans. Bootstrapping in Angular is a technique used for starting or initializing the Angular application. There are two types of bootstrapping: 

  1. Automatic Bootstrapping: 

In this, the ng-app directive is added to the root of the application. When Angular finds the ng-app directive, it loads the module associated with it and then compiles the DOM.

  1. Manual Bootstrapping: 

It gives you more control over how you initialize your application. It is used when you want to perform any other operation before Angular wakes up and compiles the page.

  1. What do you understand by $emit, $broadcast, and $on in AngularJS?

Ans. $emit, $broadcast, and $on are services in Angular Js. $broadcast() and $emit() are used to raise an event in the AngularJS application.

$emit: 

It sends an event name to the controllers up in its scope hierarchy. It notifies the registered $rootScope.Scope listeners. The event life cycle starts at the scope on which $emit was called.

$broadcast(): 

$broadcast() service triggers an event and sends data/information/message to all its child controllers down in its scope hierarchy. It notifies the registered $rootScope.Scope listeners. The event life cycle starts at the scope on which $broadcast was called.

$on(): 

It listens to any type of event raised by $broadcast and $emit by the controllers descending or preceding it. It can catch the event dispatched by $broadcast and $emit.

  1. Explain how will you set, get, and clear cookies in Angular.

Ans. In AngularJs, angular-cookies.js is used to set, get, and clear a cookie.

Set Cookies: 

To set the cookies in a key-value format, we use the Put method.

Syntax: 

$cookies.put(“username”, $scope.username);

Get Cookies: 

Get method is used to retrieve the cookies.

Syntax:

$cookies.get(‘username’);

Clear Cookies:

To clear cookies, we use the ‘remove’ method.

Syntax:

$cookies.remove(‘username’);

  1. Write a code to insert an embedded view from a prepared TemplateRef.

Ans. Below is the code to insert an embedded view from a prepared TemplateRef:

@Component({

selector: ‘app-root’,

template: `

    <ng-template #template let-name=’fromContext’><div>{{name}}</ng- 

 template>

    <ng-container #vc></ng-container>

`

})

export class AppComponent implements AfterViewChecked {

@ViewChild(‘template’, { read: TemplateRef }) _template: TemplateRef<any>;

@ViewChild(‘vc’, {read: ViewContainerRef}) vc: ViewContainerRef;

constructor() { }

 

ngAfterViewChecked() {

    const view = this._template.createEmbeddedView({fromContext: ‘Kevin’});

    this.vc.insert(view);

}

}

  1. What is the procedure to update or upgrade the Angular CLI version?

Ans. To upgrade the application, we need to enable the following commands

  • npm error cache clean or npm cache (npm> 5 if)
  • npm install -g @ angular / cli @ upgrade version
  • npm uninstall -g angular-cli
  1. Explain the difference between RouterModule.forRoot() vs RouterModule.forChild()?

Ans. RouterModule.forRoot is known as a static method, and it is used to configure the modules.

RouterModule.forChild() is also a static method but it configures the routes of lazy-loaded modules.

  1. What is the procedure to debug the router?

Ans. It can be initiated by passing a flag when it is added to the app routes.

Example:

@NgModule({

imports: [ RouterModule.forRoot(routes, { enableTracing: true }) ],

exports: [ RouterModule ]

})

export class AppRoutingModule {}

  1. What is an ng-App directive?

Ans. ng-App directive appoints the root element of an AngularJs application by defining the AngularJs application. In an HTML document, any number of ng-App directives can be defined, but only one AngularJS application can be auto-bootstrapped, and the rest need to be done automatically.

For example:

<div ng-app=””>

<p>My first expression: {{127 + 162}} </p>

</div>

  1. What is the procedure to implement multiple API calls that need to happen in order using rxjs?

Ans. Following is the procedure to implement a multiple API calls:

Method 1

this.http.get(‘/api/url’).subscribe(data => {

this.http.get(/api/url/data).subscribe(data11 => {

});

});

Method 2:

Using “MergeMap”

this.http.get(‘/api/url’).pipe(

mergeMap(character => this.http.get(character.naukrilearning))

);

  1. How to hide an HTML tag just by one button click in angular. 

Ans. We can hide an HTML tag just by one button click in angular using the following:

View

<div ng-controller=”MyController”>

  <button ng-click=”hide()”>Hide element</button>

  <p ng-hide=”isHide”>Hello World!</p>

</div>

Controller

controller: function() {

this.isHide = false;

this.hide = function(){

this.isHide = true; }; }

  1. Write a code snippet to make an AngularJS service return a promise. 

Ans. To make an AngularJS service return a promise, we inject the “$q” dependency in the service. 

angular.factory(‘testService’, function($q){

return {

getName: function(){

var deferred = $q.defer();

//API call here that returns data

testAPI.getName().then(function(name){

deferred.resolve(name)

})

return deferred.promise;

}

}

})

  1. Explain the orderby filter in AngularJS. 

Ans. The Orderby filter sorts an array based on specified criteria. The sign of the filter sets the order, (+ is ascending) while (- is descending).

Syntax: 

{{ orderBy_expression | orderBy : expression : reverse }}   

 

Example: 

The code below will orderBy on the basis of name.

<!DOCTYPE html>  

<html>  

<script src=

“https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js”>

</script>  

<body>  

<div ng-app=”myApp” ng-controller=”orderCtrl”>  

<ul>  

<li ng-repeat=”x in customers | orderBy : ‘name’”>  

    {{x.name + “, ” + x.city}}  

</li>  

</ul>  

</div>  

<script>  

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

app.controller(‘orderCtrl’, function($scope) {  

    $scope.customers = [  

        {“name” : “Akshay”, “city” : “Delhi”},  

        {“name” : “Vibhor “, “city” : “Mumbai”},  

        {“name” : “Ravi”, “city” : “Hyderabad”},  

        {“name” : “Shekhar”, “city” : “Bangalore”},    

    ];  

});  

</script>  

<p>The array items are arranged by “name”.</p>  

</body>  

</html>  

Output: 

Akshay, Delhi

Shekhar, Bangalore

Ravi, Hyderabad

Vibhor, Mumbai 

The array items are arranged by “name”

 

By bpci