Top Angular Interview Questions and Answers

Top Angular Interview Questions and Answers

Here I have collect Top Angular Interview Questions and Answers.

Angular is an application design framework and development platform for creating efficient and sophisticated single-page applications.

Following are frequently asked Angular questions to get job or increase knowledge.

What is Angular?

Angular is a development platform, built on TypeScript.

Angular includes:

  • A component-based framework for building web applications.
  • A collection of libraries that cover a wide variety of features, including routing, forms, client-server communication, and more.
  • Tools to help you develop, build, test, and update your code

What is the difference between Angular and AngularJs?

AngularAngularJS
This is based on Service/ControllerIt is based on MVC architecture
Introduced the TypeScript to write the applicationIt uses JavaScript to build the application
This is a component based UI approachBased on controllers concept
Developed considering mobile platformNot a mobile friendly framework

What is TypeScript?

Typescript is a robust typed programming language built on JavaScript that gives you better tooling on any scale. Now try the typescript. Online or by npm.

What are the key components of Angular?

Angular has the following main components:

  • Component: These are the basic building blocks of an angular application for controlling HTML views.
  • Modules: An angular module is a set of angular basic building blocks, such as components, directions, services, etc. The application is divided into logical pieces and each part of the code is called a “module” that performs the same function.
  • Samples: This presents the views of the angular application.
  • Services: Used to create components that can be shared across applications.
  • Metadata: This can be used to add more data to the angular class.

What are directives?

Directives add behaviour to an existing DOM element or an existing component instance.

import { Directive, ElementRef, Input } from '@angular/core';

@Directive({ selector: '[myBgcolor]' })
export class BgcolorDirective {
    constructor(el: ElementRef) {
       el.nativeElement.style.backgroundColor = 'yellow';
    }
}

Now this directive extends HTML element behavior with a yellow background as below

<span myBgcolor>Yellow Bg Color!</span>

What are components?

Elements is the most basic UI building block of angular application that creates a tree of angular elements. These components are a subset of directions. Contrary to the instructions, the components always have one template and only one component per element can be instant in the template.

Let us see a simple example of an angular component:

import { Component } from '@angular/core';

@Component ({
   selector: 'common-title',
   template: ` <div>
      <h1>{{title}}</h1>
   </div> `,
})

export class AppComponent {
   title: string = 'Welcome to Codeshipguru';
}

What are the differences between Component and Directive?

Component

  • To register a component we use @Component meta-data annotation
  • Components are typically used to create UI widgets
  • Component is used to break up the application into smaller components
  • Only one component can be present per DOM element
  • @View decorator or templateurl/template are mandatory

Directive

  • To register directives we use @Directive meta-data annotation
  • Directive is used to add behavior to an existing DOM element
  • Directive is use to design re-usable components
  • Many directives can be used per DOM element
    1. Directive doesn’t use View

What is a template?

A template is an HTML view where you can display data by typing restrictions to the properties of an angular component. You can store a sample of your component in one of two places. You can define it inline using the template property, or you can define the template in a separate HTML file and link it to the component metadata using the @Component decorator’s templateUrl property.
Using inline template with template syntax,

import { Component } from '@angular/core';

@Component ({
   selector: 'common-title',
   template: '
      <div>
         <h1>{{title}}</h1>
      </div>
   '
})

export class AppComponent {
   title: string = 'Hello World';
}

Using separate template file such as app.component.html

import { Component } from '@angular/core';

@Component ({
   selector: 'common-title',
   templateUrl: 'app/app.component.html'
})

export class AppComponent {
   title: string = 'Hello World';
}

What is a module?

Modules are logical boundaries in your application and the application is divided into separate modules to separate the functionality of your application. Lets take an example of app.module.ts root module declared with @NgModule decorator as below,

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent }  from './app.component';

@NgModule ({
   imports:      [ BrowserModule ],
   declarations: [ AppComponent ],
   bootstrap:    [ AppComponent ],
   providers: []
})
export class AppModule { }

The NgModule decorator has five important(among all) options

  1. The imports option is used to import other dependent modules. The BrowserModule is required by default for any web based angular application
  2. The declarations option is used to define components in the respective module
  3. The bootstrap option tells Angular which Component to bootstrap in the application
  4. The providers option is used to configure set of injectable objects that are available in the injector of this module.
  5. The entryComponents option is a set of components dynamically loaded into the view.

Thank you reading Top Angular Interview Questions and Answers. Comment and share article if you like. 

Post a Comment

0 Comments