It's the Xinrui Ma

Blog

Category: Angular 2

Angular 2 Interview Questions & Answers

1. What are Angular Directives? Explain attribute directive.
Directives are instructions which tell angular to do something.
Directives allow you to attach Behaviour to the element in the DOM.

2. How many types of Directives are there in Angular 2?
There are three types of directives in Angular2:
a. Components—directives with a template.
b. Structural directives—change the DOM layout by adding and removing DOM elements.
c. Attribute directives—change the appearance or behavior of an element, component, or another directive.

3. How event are attached to directives?
The @HostListener decorator lets you subscribe to events of the DOM element that hosts an attribute directive

4. How do we pass external data into Directive?
To pass the external data into directive it means we can pass any data from the different component into the directive and it can be done by @input data binding.
Read more.

5. How to handle HTTP error in Angular 2?
You could handle in the component by adding a second callback to the .subscribe():

6. Explain how we pass data in HTTP request?
In addition to fetching data from the server, HttpClient supports mutating requests, that is, sending data to the server with other HTTP methods such as PUT, POST, and DELETE.

How to solve “Cannot find name ‘require’ ” problem on Angular 2 project

It’s magic:

I want to include AWS library on my angular2 project, I was looking at https://github.com/aws/aws-sdk-js

Steps I follow:
1. npm install --save-dev @types/node
2. find tsconfig.app.json, edit those two MAGIC lines:
"types": [ "node" ],
"typeRoots": [ "../node_modules/@types" ]

3. npm install aws-sdk

4. I can safely add var AWS = require('aws-sdk'); in my project now.

Remeber the magic!