Angular 2 Interview Questions

Angular 2 Interview Questions

Dear friends, below are the list of Angular 2 interview questions that have been designed for Angular 2 programmers / UI Developers who are preparing interviews on Angular2 interviews. Here, we have added some basic and advanced or both questions on Angular 2 that are asked in 2018 and that help you to crack interview on Angular 2.0. You can also download latest Angular2 interview questions and answers pdf for freshers and experienced.

Angular2 is a free open source component-based UI Framework from google web development team re-written in TypeScript (Superset of JavaScript).

Below are the list of Top 37 Angular 2.0 interview questions and answers.

Download Angular 2 Interview Questions PDF

Angular 2 Interview Questions

The hidden property in Angular 2 is a special case.

  • The property is more powerful and is used to bind any property of the elements.
  • It is considered the closest cousin of ngshow and nghide.
  • It sets the display property “display: none”.

Components in Angular 2 are simply directives that are always associated with a direct template. Angular 2 components have an extremely well defined life-cycle. When working with angular components, we can make use of interfaces which allows us to implement functionality for different times in a components lifecycle. A component must belong to an NgModule in order for it to be usable by another component or application. Components can even control their runtime behaviour by implementing various Life-cycle hooks.

Declaration of component:

@component ({selector: 'great', template: 'hello {{name}}!'})

Class greet{

Name: string = 'world';

}

Components always have a template and only one component can be instantiated per an element in a template. When a component is instantiated, angular creates a change detector, which is responsible for propagating the component’s building.

Angular 2 is a platform that encompasses a wide range of capabilities. Some new features were added in Angular 2 which includes:

  • Universal server rendering- It is the library which is used to make building universal apps a smooth experience. It is an important feature of Angular 2.
  • A mobile toolkit- It provides all the mobile toolkit and techniques to build high-performance mobile applications. The web applications which are developed using the mobile toolkit can be loaded on any device with or without internet connection which is a great advantage.
  • A command line interface-it can generate components, routes, services, and pipes with the help of commands.
  • Data binding- data binding has been improved in Angular 2. So, whatever DOM element property you need to bind, you just wrap it in square brackets. E.g.-
    <img[src]='product.image' />
  • Modular- various modules have been removed from angular’s core, which has resulted in better performance.
  • Modern- Angular 2 has been targeted as modern browsers in which various hacks that make angular harder to develop have been removed.

Structural directives are used to manipulate DOM in angular. Structural directives are responsible for HTML layout. By adding, removing, or manipulating LMNs in angular, they shape or reshape the structure of DOM. This structural directive is applied to a host element with the help of other directives. The directives then do whatever it is supposed to do with that host element and its descendants. Structural directives can be easily recognized. It can also delay the instantiation of a component or an element. It can also be used for cosmetic effect or manually handling the timing of the loading of components. Structural directives are bound to a template. The two most common structural directives are “ngIf” and “ngFor”. The process occurring in a structural directive is dynamic.

CLI is the acronym of Command Line Interface, which can be used to create the Angular JS application.Using CLI, you can also create a unit and end-to-end tests for the Angular application.

The Angular 2 comprises of the following key components:

  • Module – This is used to break the application into the logical pieces of the program code and each piece of code or module is designed to perform a single and unique task.
  • Component – This is used to bring the modules together.
  • Templates – This is used to define the Views of an Angular JS application.
  • Metadata – This is used to add more data to an Angular JS application.
  • Service – This component is used to develop the components, which can be used to share in the entire application.

Angular 2 is a re-written version of Angular1 and not an update. The best way to compare Angular 2 and Angular 1 is by finding out the new features in Angular 2. This way we will be able to find out the advantages of Angular 2 over Angular1 precisely. So, some of the advantages of Angular 2 are:-

Angular 2Angular1
Angular 2 is a mobile-oriented frameworkWhereas Angular1 was not developed with mobile base in mind.
Angular 2 is a versatile framework, i.e. we have more choices for languages. We can use ES5, ES6, Typescript or Dart to write an Angular 2 codeWhereas an Angular1 code can written by using only ES5, ES6 and Dart. We don’t have many choices of language in Angular1.
Nowadays, the controllers are replaced by components and Angular 2 is completely component based.Whereas Angular1 was based on controllers whose scope is now over.

Angular 2 directly uses the valid HTML DOM element properties and events which reduces the usage of many available built-in directives.

In Angular 2, decorators are used as an identifier of class or type of the object that is created by the TypeScript.The Angular 2 identifies the class below decorator call as the definition of the class and extends the decorator specific properties with the class definition.

AppModule is required for every Angular 2 app.

The Angular 2 Applications provide with the option of error handling.The errors in Angular 2 can be handled by including the ReactJS catch library and later using the catch function.

  • The catch function, which is used after adding the catch library contains the link to the Error handler function.
  • And in this error, handler function, the errors are sent to the error console, and also the errors are thrown back to continue the execution.
  • So, whenever an error occurs it will be redirected to the error console of the web.

Shared module is used to import the services in both eager and lazy loaded module. We all know that lazy loaded modules create their own branch on the dependency injection tree. Shared module consists of the services that are registered by the angular in the root app injector. For this, we need not import it in the lazy module because lazy loaded modules already have access to the services defined at the root. Components, pipes and directives are also defined in the shared module. Other modules that import the shared module can use it in their templates. This means that the modules can be imported normally in the lazy loaded module. The shared module contains the code that will be used across the applications and featured modules. It also consists of the common template components. “Dumb components” should also be present in the shared module. It typically consists of some common angular modules too. When you are importing the shared module, you will also need to import the module with its providers, because there is no app module in the test.

Angular 2 is a completely revived component-based Javascript framework in which an application is a tree of loosely coupled components. It is a more advanced version of angularJS. It is more of an "all in one" framework so it also helps in creating a single website without getting trapped into different JS frameworks. An Angular 2 is a modular framework in which our code is divided into individual procedures that offer a similar kind of functionality, hence improving the testing, up gradation and maintenance of the application. It has a lot of useful features such as- server-side rendering, cross-platform, and supports more languages than any other framework. It is a new typescript framework built around the concept of components which is paving the way for a better and spacious development. We can even make hybrid applications using Angular 2 which gives us a sharp edge by providing us the flexibility to use the same standard codes for developing other applications.

The component lifecycle hooks overview the life cycle sequence and the interfaces. Angular manages the life cycle of a component. Angular creates it, renders it. It can also create and render its children. It also checks when its data-bound properties change. It can even destroy it before removing it from the DOM. The life cycle hook offered by angular provides the visibility into these key life moments and the ability to act when they occur. The components go through an entire set of processes or life cycle right from its initiation to the end of the application.

There are a number of lifecycle hooks which are listed below:–

  1. ngOnChanges
  2. ngOnInit
  3. ngDoCheck
  4. ngAfterContentInit
  5. ngAfterContentChecked
  6. ngAfterViewInit
  7. ngAfterViewChecked
  8. ngOnDestroy

Below are few component specific hooks in Angular2.

  • ngafterContentinit: It initializes the component content
  • ngAfterConctentChecked: It checks the binding of the external content.
  • ngafterViewinit: It creates the component view.
  • ngAfterviewChecked: It checks the bindings of the component’s view.