Git
Git
Git
Answers
Manav Pandya 13 min read
08 Jan 2019
125
Angular is a JavaScript-based framework developed by Google. Angular allows developers to build applications for
browsers, mobile, and desktop using web technologies like HTML, CSS, and JavaScript. This article contains the top 20
Angular interview questions and answers, in order to prepare you for the interview.
Angular2+ is different from Angular1.x and it is completely re-written from scratch using Typescript. At the
time of writing this, book the latest version of Angular was 7 which was released on 19 Oct 2018.
There are following differences between Angular2+ and Angular 1.x are given below:
Angular 2+ Angular 1.x
ased on Based on controller and scope
omponents
mproved DI Supports DI
he class is the factory, service, provider, value and constant are used for services
nly way to define
ervices in
Angular2
upports Supports spinal-case and camelCase syntaxes like ng-model, ng-class and ngModel
amelCase and
ascalCase
yntaxes
kengModel,
gForm and
NgForm
Use Html DOM Uses its own directives like ng-click, ng-show and ng-src etc.
lements
roperties and
vents
There are multiple reasons to use Angular over other front-end frameworks which are listed below.
Angular has large numbers of feature which makes it more useful over other front-end frameworks, below
is the list of important features.
Component
Module
Data bindings
Services
Dependency Injection
Directives
Templates
Application Bootstrapping
Navigation
Recently Angular has released its newer version Angular 7, and with this version, we got several updated
as well as the new features and these are listed below.
CLI Prompt
Angular Elements
Virtual scrolling
Setting budgets
If you want to work on the latest version of the Angular then we need to update our Angular CLI as well as
Core angular packages by using below command.
Angular Command Line Interface is a command line tool which is used to create, initialize, scaffold and to
manage the whole angular application. We can also use Angular Console tool to work with the command
line to generate or work with different part of the Angular application, using CLI we can manage
everything using commands.
You can find the complete details of Angular CLI and different commands into the CLI introduction part
later on this series.
Angular CLI is a powerful command-line tool by which we can create new files, update the file, build and
deploy the angular application and other features are available so that we can get started with Angular
development within few minutes.
Various code generation options for Component, Module, Class, Pipes, Services, Enums, and many other
types are also supported.
Angular CLI is a powerful tool and by using this we can develop an Angular application without following
complex stuff. Angular Console is a way to achieve different command line features using a simple GUI
which is completely alternative to know all of the CLI commands. To download the GUI tool, you can get
the executable file from here. And after downloading the tool, it will look like this.
To get the current version of the Angular CLI, we can use below command.
ng –version
After running above command, we will get the output like this.
Q13. How to specify the type of the stylesheet using the command?
Angular 7 provides the CLI prompt where we can choose stylesheet type from the available option, but for
an older version, CSS was the default format for styling the pages. But for that, we can use additional flag -
-style to specify any other supported stylesheet type than css, below is the simple example by which we
can use specific stylesheet format while creating a new Angular app.
Example
ng new ngDemo --styles scss
After running the above command, now our default stylesheet format will be SCSS, not the CSS, this is
how we can change the stylesheet type.
Components are the basic building block of an Angular application. An Angular component mainly
contains a template, class, and metadata. It is used to represent the data visually. A component can be
thought as a web page. An Angular component is exported as a custom HTML tag like as: <my-app>
</my-app>.
When we create new create an Angular app using CLI, it will create default App component as the entry
point of application. To declare a class as a Component, we can use the Decorators and decorator to
declare a component looks like the below example.
@Component({
selector: 'my-app',
template: `<h1></h1>`,
styleUrls: ['./app.component.css']
})
exportclassAppComponent {
message = "Hello From Angular";
}
For creating a component, we can use decorator @component like the above example. If we are using
Angular CLI than new component will be created using the below ng command.
Template expressions are the same expression that we use with JavaScript. But the difference is that we
can use it within our Html template, so it looks like that we are using JavaScript along with Html but keep
in mind that not every JavaScript expressions are supported like (new), (++) and (--). Let’s see a simple
example of the mathematical calculation using template expression.
App.component.html
<p>Addition : 2</p>
<p>Subtraction : -25</p>
<p>Multiplication : 1250</p>
<p>Division : 0.5</p>
Here we have used brackets in order to use an expression within our template, and you can see the output
like this.
Data binding is one of the important core concepts which is used to do the communication between the
component and DOM. In other words, we can say that the Data binding is a way to add/push elements
into HTML Dom or pop the different elements based on the instructions given by the Component. There
are mainly three types of data binding supported in order to achieve the data bindings in Angular.
Event Binding
Two-way Binding
Using these different binding mechanisms, we can easily communicate the component with the DOM
element and perform various operations.
Interpolation in Angular is used to show the property value from component to the template or used to
execute the expressions. Generally, interpolation can be used to execute the JavaScript expressions and
append the result to the Html DOM. Another use of Interpolation is to print the data or we can say one-
way data, which is coming from the component i.e. from a variable or the item of an array. And
interpolation can be used to achieve Property binding as well by using curly braces , let’s see a simple
example.
App.component.ts
myMessage: string = "Hello World";
App.component.html
<p></p>
<p>Total : 250</p>
The same way, we can use interpolation to the different places or elements like the source of the image,
values for the classes, and other use as well. The output will look like this.
Property binding is generally used to show or update the value of the variables in component and vice
versa. We can also call it one-way data binding thus it is not used to update the two-way binding
mechanism, and value will be reflected at the component level. Let’s see the simple example by taking
simple string value from component to the template.
App.component.ts
We are going to create a string variable with a default value like this.
And to show this message into the template, we will use a symbol which is called interpolation like this.
App.component.html
<h1></h1>
Whenever we change the value of the myMessage variable, at that time it will be reflected in the template
but not vice versa.
The directive in Angular basically a class with the (@) decorator and duty of the directive is to modify the
JavaScript classes, in other words, we can say that it provides metadata to the class. Generally, @Decorator
changes the DOM whenever any kind of actions happen and it also appears within the element tag. The
component is also a directive with the template attached to it, and it has template-oriented features. The
directive in Angular can be classified as follows.
01. Component Directive
02. Structural Directive
03. Attribute Directive
Q20. What is Module in Angular?
The module in Angular is a very essential part of the application, which is used to organize our angular
app into a distributive manner. Or we can say that Modules plays an important role to structure our
Angular application. Each Angular application must have one parent module which is called app module
and it contains the list of different items which are listed below.
Components
Service providers
Custom modules
Routing modules
Pipes
Module file group all of them into a single file; the same module will be useful for bootstrapping the
application.
Summary
I hope these questions and answers will help you to crack your Angular2+ Interview. These interview Questions have been
taken from our new released eBook Angular Interview Questions & Answers. This book contains more than 175 Angular
interview questions.
This eBook has been written to make you confident in Angular with a solid foundation. Also, this will help you to turn your
programming into your profession. It's would be equally helpful in your real projects or to crack your Angular Interview.