Angular Interview Questions
Angular Interview Questions
1. What is Angular?
Back in the day, web developers used VanillaJS and jQuery to develop dynamic
websites but, as the logic of one's website grew, the code became more and more
tedious to maintain. For applications that use complex logic, developers had to put in
extra effort to maintain the separation of concerns for the app. Also, jQuery did not
provide facilities for data handling across views.
For tackling the above problems, client-side frameworks like Angular came into
the picture, which made life easier for the developers by handling the separation of
concerns and dividing code into smaller bits of information (In the case of Angular,
called Components).
Client-side frameworks allow one to develop advanced web applications like Single-
Page-Application. Not that we cannot develop SPAs using VanillaJS, but by doing
so, the development process becomes slower.
Every Angular app consists of a file named angular.json. This file will contain all the
configurations of the app. While building the app, the builder looks at this file to find
the entry point of the application. Following is an image of the angular.json file:
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/angular-starter",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.app.json",
"aot": false,
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"./node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css",
"src/style.css"
]
}
}
Inside the build section, the main property of the options object defines the entry
point of the application which in this case is main.ts.
The main.ts file creates a browser environment for the application to run, and, along
with this, it also calls a function called bootstrapModule, which bootstraps the
application. These two steps are performed in the following order inside the main.ts
file:
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule
],
providers: [],
entryComponents: [],
bootstrap: [AppComponent]
})
export class AppModule { }
This component is defined in app.component.ts file. This file interacts with the
webpage and serves data to it.
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'angular';
}
After this, Angular calls the index.html file. This file consequently calls the root
component that is app-root. The root component is defined in app.component.ts.
This is how the index.html file looks:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Angular</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<app-root></app-root>
</body>
</html>
The HTML template of the root component is displayed inside the <app-root> tags.
• Features that are provided out of the box - Angular provides a number of built-in
features like routing, state management, rxjs library and http servicesstraight out of
the box. This means that one does not need to look for the above-stated features
separately. They are all provided with angular.
• Declarative UI - Angular uses HTML to render the UI of an application. HTML is a
declarative language and is much easier to use than JavaScript.
• Long-term Google support - Google announced Long-term support for Angular.
This means that Google plans to stick with Angular and further scale up its
ecosystem.
Angular React
Angular can be used in both mobile and web React can only be used in UI
development. development only.
Check out the differences between AngularJS and Angular below. For More
Information, Click here.
7. What is the main thing that you would need to change if you were migrating
from AngularJS 1.4 to AngularJS 1.5?
Yes.
a. Scope
b. Controller
c. Model
d. View
e. Services
f. Data Binding
g. Directives
h. Filters
i. Testable
The scope is a unique JavaScript object that plays the role of joining the controller
(JavaScript) with the views (HTML). The controller sets properties on the scope,
and the view binds to them.
Each AngularJS application has only one root scope. Child controllers, however,
create scope for each view. When the new scopes are created, they are added to
their parent root scope as child scopes. This creates a hierarchical structure when
they are attached.
You can use a directive as an element, attribute, or class name. To define the way
your directive will be used, you need to set the restrict option in the directive
declaration. The restrict option can be set to:
During every digest cycle, all new scope model values are compared against the
previous values. This is called dirty checking. If change is detected, watches set
on the new model are fired, and another digest cycle executes. This goes on until
all models are stable.
The digest cycle is triggered automatically, but it can be called manually using
“.$apply().”
14. What are the common ways of communication between modules of your
application, using core AngularJS functionality?
The common ways of communication include:
a. Using events
b. Using services
15. Explain what the link function is and how it differs from compile.
The link function combines the directives with a scope to produce a live view.
The link function is responsible for instance DOM manipulation and for registering
DOM listeners.
The compile function is responsible for template DOM manipulation as well as the
collection of all the directives.
Dependency injection is the process where the dependent objects are injected
rather than being created by the consumer.
SPA is the concept whereby pages are loaded from the server not by doing
postbacks, instead of by creating a single shell page or master page and loading
the web pages into the master page.
20. How can a SPA be implemented in AngularJS?
b. Work in batches
c. Cache DOM
Example:
<div ng-app="">
<p>My first expression: {{157 + 122}} </p>
</div>
<element ng-switch="expression">
<element ng-switch-when="value"></element>
...
</ANY_HTML_ELEMENT>
By default, the included file must be located on the same domain as the document.
<div ng-include="'myFile.htm'"></div>
ng-click can be used in scenarios like when you click on a button or when you want
to do any operation. It tells AngularJS what to do when an HTML element is clicked.
Example:
<button ng-click="count = count + 1" ng-init="count=0">OK</button>
The above code will increase the count variable by one whenever the button is
clicked.
Syntax:
serviceApp.provider("logService", function ())
<p>
</p>
<p>
</p>
<p>
{{ mySwitch }}
</p>
</div>
36) What are the attributes that can be used during the creation of a new
AngularJs directives?
Answer: There are several attributes that can be used during a new directive
creation.
They include:
1. Template: It describes an inline template as a string.
2. Template URL: This attribute specifies the AngularJs HTML compiler to
replace the custom directive inside a template with the HTML content located
inside a separate file.
3. Replace: It replaces the current element if the condition is true if false append
this directive to the current element.
4. Transclude: It allows you to move the original children of a directive to a
location inside the new template.
5. Scope: It creates a new scope for this directive rather than inheriting the
parent scope.
6. Controller: It creates a controller which publishes an API for communicating
across the directives.
7. Require: It requires another directive to be present to function the current
directive efficiently.
8. Link: It modifies resulting in DOM element instances, adds event listeners,
and set up data binding.
9. Compile: It modifies the DOM template for features across copies of a
directive, as when used in other directives. Your compile function can also
return link functions to modify the resulting element instances.
42) If both factory and service are equivalent, then when should I use them?
Answer: Factory provider is preferred using an object, whereas a service provider is
preferred using with class.
ng-show directive is used to show the HTML element if the expression becomes
true. And if the expression becomes false then the HTML element will be hidden.
Syntax
<element ng-show=”expression”></element>
ng-hide directive is used to hide the HTML element if the expression becomes false.
Syntax
<element ng-hide=”expression”></element>
Answer: Global API is the combination of global JavaScript function, which is used to
perform tasks such as comparing objects, iterating objects, and converting the data.