ANGULAR 7 Interview Questions and Answers

Angular7 is a TypeScript-based open-source front-end platform that makes it easy to build applications with in web/mobile/desktop. The major features of this framework such as declarative templates, dependency injection, end to end tooling, and many more other.

11) What is angular CLI?

Angular CLI(Command Line Interface) is a command line interface to scaffold and build angular apps using nodejs style (commonJs) modules. You need to install using below npm command:

npm install -g @angular/cli

Below are the list of few commands, which will come handy while creating angular projects:

       Creating New Project Command: ng new folder_name

       Generating Components, Directives & Services: ng generate/g The different types of commands would be:

           ng generate component my-new-component: add a component to your application

           ng generate directive my-new-directive: add a directive to your application

           ng generate enum my-new-enum: add an enum to your application

           ng generate module my-new-module: add a module to your application

          ng generate pipe my-new-pipe: add a pipe to your application

          ng generate service my-new-service: add a service to your application

Running the Project: ng serve

12) What is the difference between constructor and ngOnInit?

TypeScript classes has a default method called constructor which is normally used for the initialization purpose. Whereas ngOnInit method is specific to Angular, especially used to define Angular bindings. Even though constructor getting called first, it is preferred to move all of your Angular bindings to ngOnInit method. In order to use ngOnInit, you need to implement OnInit interface as below:

export class App implements OnInit{
     //called first time before the ngOnInit()

     //called after the constructor and called  after the first ngOnChanges()

13) What is a service?

A service is used when a common functionality needs to be provided to various modules. Services allow for greater separation of concerns for your application and better modularity by allowing you to extract common functionality out of components. Let's create a repoService which can be used across components:

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';

@Injectable() // The Injectable decorator is required for dependency injection to work
export class RepoService{
  constructor(private http: Http){

    return this.http.get('').map(res => res.json());

The above service example uses Http service as a dependency.

14) What is the purpose of ngFor directive?

We use Angular ngFor directive in the template to display each item in the list. For example, here we iterate over list of users:

<li *ngFor="let user of users">
  {{ user }}

15) What is the purpose of ngIf directive?

Sometimes an app needs to display a view or a portion of a view only under specific circumstances. The Angular ngIf directive inserts or removes an element based on a truthy/falsy condition. Let's take an example to display a message if the user age is more than 18:

<p *ngIf="user.age > 18">You are not eligible for student pass!</p>

16) What is interpolation?

Interpolation is a special syntax that Angular converts into property binding. It’s a convenient alternative to property binding. It is represented by double curly braces({{}}). The text between the braces is often the name of a component property. Angular replaces that name with the string value of the corresponding component property. Let's take an example:

  <img src="{{url}}" style="height:30px">

In the example above, Angular evaluates the title and url properties and fills in the blanks, first displaying a bold application title and then a URL.

17) What is router outlet?

The RouterOutlet is a directive from the router library and it acts as a placeholder that marks the spot in the template where the router should display the components for that outlet. Router outlet is used like a component:

<!-- Routed components go here -->

18) What is an observable?

An Observable is a unique Object similar to a Promise that can help manage async code. Observables are not part of the JavaScript language so we need to rely on a popular Observable library called RxJS. The observables are created using new keyword. Let see the simple example of observable:

import { Observable } from 'rxjs';

const observable = new Observable(observer => {
  setTimeout(() => {'Hello from a Observable!');
  }, 2000);

19) What is RxJS?

RxJS is a library for composing asynchronous and callback-based code in a functional, reactive style using Observables. Many APIs such as HttpClient produce and consume RxJS Observables and also uses operators for processing observables. For example, you can import observables and operators for using HttpClient as below:

import { Observable, throwError } from 'rxjs';
import { catchError, retry } from 'rxjs/operators';

    20) What is HttpClient and its benefits?

    Most of the Front-end applications communicate with backend services over HTTP protocol using either XMLHttpRequest interface or the fetch() API. Angular provides a simplified client HTTP API known as HttpClient which is based on top of XMLHttpRequest interface. This client is avaialble from @angular/common/http package. You can import in your root module as below:

    import { HttpClientModule } from '@angular/common/http';
    The major advantages of HttpClient can be listed as below:

    1. Contains testability features
    2. Provides typed request and response objects
    3. Intercept request and response
    4. Supports Observalbe APIs
    5. Supports streamlined error handling