Angular Questions
Angular Questions
Angular 4 is a Javascript framework built around the concept of components, and more
precisely, with the Web Components standard in mind. It was rewritten from scratch by
the Angular team using Typescript (although we can use it with ES5, ES6, or Dart as
well).
https://medium.com/@vigowebs/frequently-asked-angular-interview-questions-and-answers-d996be87cc7c 1/10
9/23/2019 Frequently asked: Angular Interview Questions and Answers
Angular 4 uses TypeScript. TypeScript will not be used in the browser directly. So
the program code is compiled to JavaScript. This can be achieved with “Traceur”.
The digest cycle from Angular 1.x has been replaced by another internal
mechanism known as “Change Detection”. This feature, along with other
improvements and tweaks, yields a considerable increase in performance.
Unlike Angular 1.x where we can get most of the functionalities in angular.js file,
Angular 4 follows module pattern. We need to import the functions ourself and
export them when we need anywhere else.
There are no more factory , service , provider in Angular 4. We need to use class
The main objectives of decorators is to add some metadata to the class that will tell
Angular 4 how to process a class. Or in another words, Decorators are functions that
modify JavaScript classes. Angular has many decorators that attach metadata to classes
so that it knows what those classes mean and how they should work.
selector: — define the name of the HTML element in which our component will
live.
styles: — the styles array for our specific component. We can also link external CSS
by styleUrls.
providers: — This is the place we are passing the services that we need insider our
components.
Immediately after this decorator or right to it, we need to export a class where our
variables and functions reside that our component uses.
https://medium.com/@vigowebs/frequently-asked-angular-interview-questions-and-answers-d996be87cc7c 2/10
9/23/2019 Frequently asked: Angular Interview Questions and Answers
Q3. What is compilation in Angular 4? And what are the types of compilation in
Angular 4?
There is actually only one Angular compiler. The difference between AOT and JIT is a
matter of timing and tooling. There are two types of compilation Angular 4 provides.
Ahead-of-time (AOT) compilation: With AOT, the compiler runs at the build time
and the browser downloads only the pre compiled version of the application. The
browser loads executable code so it can render the application immediately,
without waiting to compile the app first. This compilation is better than JIT
because of Fast rendering, smaller application size, security and detect template
errors earlier.
An NgModule class describes how the application parts fit together. Every application
has at least one NgModule , the root module that we bootstrap to launch the application.
https://medium.com/@vigowebs/frequently-asked-angular-interview-questions-and-answers-d996be87cc7c 3/10
9/23/2019 Frequently asked: Angular Interview Questions and Answers
Here the AppComponent is the root module of our application that Angular creates and
inserts it into the index.html page.
Q5. What are all the metadata properties of NgModule ? And what are they used
for?
@NgModule accepts a metadata object that tells Angular how to compile and launch the
application. The properties are:
imports – Modules that the application needs or depends on to run like, the
BrowserModule that every application needs to run in a browser.
bootstrap – the root component that Angular creates and inserts into the
index.html host web page. The application will be launched by creating the
components listed in this array.
In the above code the #name declares a variable on the input element. Here the name
refers to the input element. Now we can access any property of the input DOM, using
this reference variable. For example, we can get the value of the input element as
https://medium.com/@vigowebs/frequently-asked-angular-interview-questions-and-answers-d996be87cc7c 4/10
9/23/2019 Frequently asked: Angular Interview Questions and Answers
Structural directives are responsible for HTML layout. They shape or reshape the
DOM’s structure, typically by adding, removing, or manipulating elements. Structural
directives are easy to recognize. An asterisk (*) precedes the directive attribute name
as in this example.
The ngFor directive iterates over the component's names array and renders an instance
of this template for each name in that array.
Some of the other structural directives in Angular are ngIf and ngSwitch .
Directives allow us to attach behavior to elements in the DOM, for example, doing
something on mouse over or click. In Angular, a Directive decoraor ( @Directive ) is
used to mark a class as an Angular directive and provides additional metadata that
determines how the directive should be processed. Below are the metadata properties
of a directive.
host - map of class property to host element bindings for events, properties and
attributes
outputs - list of class property names that expose output events that others can
subscribe to
https://medium.com/@vigowebs/frequently-asked-angular-interview-questions-and-answers-d996be87cc7c 5/10
9/23/2019 Frequently asked: Angular Interview Questions and Answers
There are three types of directives in Angular. They are attribute directives, structural
directives, and components.
Structural directives change the DOM layout by adding and removing DOM
elements. For example, *ngIf and *ngFor
Services encapsulates business logic and separates them from UI concerns or the
controller concerns, which governs them both.
Pure pipes are stateless that flow input date without remembering anything or
causing detectable side-effects. Pipes are pure by default, hence most pipes are pure.
We can make a pipe impure by setting its pure flag to false . Angular executes a pure
pipe only when it detects a pure change to the input value. A pure change is either a
change to a primitive input value or a changed object reference.
Impure pipes are those which can manage the state of the data they transform. A pipe
that creates an HTTP request, stores the response and displays the output, is a impure
https://medium.com/@vigowebs/frequently-asked-angular-interview-questions-and-answers-d996be87cc7c 6/10
9/23/2019 Frequently asked: Angular Interview Questions and Answers
or stateful pipe. Stateful Pipes should be used cautiously. Angular provides AsyncPipe ,
which is stateful. In the following code, the pipe only calls the server when the request
URL changes and it caches the server response. The code uses the Angular http client to
retrieve data:
Redux is an application state manager for JavaScript applications, and keeps with the
core principles of the Flux-architecture by having a unidirectional data flow in our
application. Redux applications have only one global, read-only application state. This
state is calculated by “reducing” over a collection or stream of actions that update it in
controlled ways.
@ngrx is a set of modules that implement the same way of managing state as well as
some of the middleware and tools in the Redux ecosystem. In other way, ngrx is a
https://medium.com/@vigowebs/frequently-asked-angular-interview-questions-and-answers-d996be87cc7c 7/10
9/23/2019 Frequently asked: Angular Interview Questions and Answers
Using this technique, we keep our application state in Store and everything saved in the
store is read only. The only way to change the state is to emit an action, an object
describing what happened.
Q13. How to prevent security threads in Angular App? What are all the ways we
could secure our App?
If using external HTML which is coming from database or somewhere outside the
application, sanitize it before using.
Try not to put external urls in the application unless it is trusted. Avoid url re-
direction unless it is trusted.
Try to prevent XSRF attack by restricting the api and use of the app for known or
secure environment/browsers.
Consider lazy loading instead of fully bundled app if the app size is more.
Make sure that any 3rd party library, which is not used, is removed from the
application.
Make sure the application is bundled, uglified, and tree shaking is done.
Q15. What is NgZone service? How Angular is notified about the changes?
https://medium.com/@vigowebs/frequently-asked-angular-interview-questions-and-answers-d996be87cc7c 8/10
9/23/2019 Frequently asked: Angular Interview Questions and Answers
Angular to track the start and completion of asynchronous activities and perform tasks
as required (e.g. change detection). Zone.js provides a global zone that can be forked
and extended to further encapsulate/isolate asynchronous behaviour, which Angular
does so in its NgZone service, by creating a fork and extending it with its own
behaviours.
The NgZone service provides us with a number of Observables and methods for
determining the state of Angular's zone and to execute code in different ways inside
and outside Angular's zone.
NgZone exposes a set of Observables that allow us to determine the current status, or
stability, of Angular's zone.
onUnstable – Notifies when code has entered and is executing within the Angular
zone.
onStable – Notifies when the last onMicroTaskEmpty has run, implying that all tasks
have completed and change detection has occurred.
We can inject the NgZone service in our component/services/etc. and can subscribe to
these observables.
https://medium.com/@vigowebs/frequently-asked-angular-interview-questions-and-answers-d996be87cc7c 9/10
9/23/2019 Frequently asked: Angular Interview Questions and Answers
Subscribing to these can help you determine if your code is unexpectedly triggering
change detection as a result of operations that do not affect application state.
http://blog.vigowebs.com/post/2018/angular5-interview-questions/
https://medium.com/@vigowebs/frequently-asked-angular-interview-questions-and-answers-d996be87cc7c 10/10