Basics Flashcards

1
Q

What are decorators in Angular?

A

Decorators are a design pattern or functions that define how Angular features work. They are used to make prior modifications to a class, service, or filter. Angular supports four types of decorators, they are:

  1. Class Decorators
  2. Property Decorators
  3. Method Decorators
  4. Parameter Decorators
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are Templates in Angular?

A

A template is a form of HTML that tells Angular how to render the component.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the differences between an Annotation and a Decorator in Angular?

A

Annotations in Angular are used for creating an annotation array. They are the metadata set on the class that is used to reflect the Metadata library.

Although Annotations and Decorators both share the same @ symbol in Angular, they both are different language features.

Annotations: These are hard-coded language feature. Annotations are only metadata set on the class that is used to reflect the metadata library. When user annotates a class, the compiler creates an attribute on that class called annotations, stores an annotation array in it, then tries to instantiate an object with the same name as the annotation, passing the metadata into the constructor. Annotations are not predefined in AngularJs so we can name them on our own.

Example:
@ComponentAnnotation
Features of Annotations:

Annotations are hard-coded.
Annotations are used by AtScript and Traceur compiler.
Annotations reflect metadata library
Note: Nowadays AngularJs switched from AtScript to TypeScript but Annotations are supported these days also.

Example: Here component is annotated as ComponentAnnotation and further used.

import {
ComponentAnnotation as Component,
} from ‘@angular/core’;

export class ComponentAnnotation extends DirectiveMetadata {

constructor() {

}
}

Decorators: A decorator is a function that adds metadata to a class, its members, or its method arguments. A decorator is just a function that gives you access to the target that needs to be decorated. There are four type of decorators all of them arem mentioned below:

Types of Decorators:

Class decorators like @Component, @NgModule
Property decorators like @Input and @Output
Method decorators like @HostListener
Parameter decorators like @Injectable
Features of Decorators:

Decorators are predefined in AngularJs.
Decorators are used by TypeScript compiler.
Decorators are used to attach metadata to a class,objects and method.
Example:

import { Component } from ‘@angular/core’;

@Component({
  selector: 'hi',
  template: '<div>GeeksForGeeks</div>',
})
export class Geeks{
  constructor() {
    console.log('GeeksForGeeks');
  }
}
Differences between Annotation and Decorator:

Annotation Decorator
Used by Traceur compiler Used by Typescript compiler
Annotations are only metadata set on the class using the Reflect Metadata library. Decorator corresponds to a function that is called on the class.
Annotations are used for creating an attribute annotations that stores array. Decorator is a function that gets the object that needs to be decorated.
They are Hard-coded They are not Hard-coded
Exp. Imports for Annotations: import {ComponentAnnotation as Component} from ‘@angular/core’; Exp. Imports for Decorators: import {Component} from ‘@angular/core’;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are Annotations in Angular?

A

Annotations in Angular are used for creating an annotation array. They are the metadata set on the class that is used to reflect the Metadata library.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are Directives in Angular?

A

Directives are attributes that allow the user to write new HTML syntax specific to their applications. They execute whenever the Angular compiler finds them in the DOM. Angular supports three types of directives.

  1. Component Directives
  2. Structural Directives
  3. Attribute Directives
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is an AOT compilation? What are its advantages?

A

The Ahead-of-time (AOT) compiler converts the Angular HTML and TypeScript code into JavaScript code during the build phase, i.e., before the browser downloads and runs the code.
Some of its advantages are as follows.
1. Faster rendering
2. Fewer asynchronous requests
3. Smaller Angular framework download size
4. Quick detection of template errors
5. Better security

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are Components in Angular?

A

Components are the basic building blocks of the user interface in an Angular application. Every component is associated with a template and is a subset of directives. An Angular application typically consists of a root component, which is the AppComponent, that then branches out into other components creating a hierarchy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are Pipes in Angular?

A
Pipes are simple functions designed to accept an input value, process, and return as an output, a transformed value in a more technical understanding. Angular supports several built-in pipes. However, you can also create custom pipes that cater to your needs. 
Some key features include: 
1.	Pipes are defined using the pipe “|” symbol. 
2.	Pipes can be chained with other pipes.
3.	Pipes can be provided with arguments by using the colon (:) sign.
15. What is the PipeTransform interface?
As the name suggests, the interface receives an input value and transforms it into the desired format with a transform() method. It is typically used to implement custom pipes.
import { Pipe, PipeTransform } from '@angular/core';
 @Pipe({
  name: 'demopipe'
})
export class DemopipePipe implements PipeTransform {
  transform(value: unknown, ...args: unknown[]): unknown {
    return null;
  }

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are Pipes in Angular?

A
Pipes are simple functions designed to accept an input value, process, and return as an output, a transformed value in a more technical understanding. Angular supports several built-in pipes. However, you can also create custom pipes that cater to your needs. 
Some key features include: 
1.	Pipes are defined using the pipe “|” symbol. 
2.	Pipes can be chained with other pipes.
3.	Pipes can be provided with arguments by using the colon (:) sign.
15. What is the PipeTransform interface?
As the name suggests, the interface receives an input value and transforms it into the desired format with a transform() method. It is typically used to implement custom pipes.
import { Pipe, PipeTransform } from '@angular/core';
 @Pipe({
  name: 'demopipe'
})
export class DemopipePipe implements PipeTransform {
  transform(value: unknown, ...args: unknown[]): unknown {
    return null;
  }

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. What are Pure Pipes?
A

These pipes are pipes that use pure functions. As a result of this, a pure pipe doesn’t use any internal state, and the output remains the same as long as the parameters passed stay the same. Angular calls the pipe only when it detects a change in the parameters being passed. A single instance of the pure pipe is used throughout all components.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are Impure Pipes?

A
For every change detection cycle in Angular, an impure pipe is called regardless of the change in the input fields. Multiple pipe instances are created for these pipes. Inputs passed to these pipes can be mutable. 
By default, all pipes are pure. However, you can specify impure pipes using the pure property, as shown below.
@Pipe({
  name: 'demopipe',
  pure : true/false 
})
export class DemopipePipe implements PipeTransform {
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is an ngModule?

A

NgModules are containers that reserve a block of code to an application domain or a workflow. @NgModule takes a metadata object that generally describes the way to compile the template of a component and to generate an injector at runtime. In addition, it identifies the module’s components, directives, and pipes, making some of them public, through the export property so that external components can use them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are filters in Angular? Name a few of them.

A

Filters are used to format an expression and present it to the user. They can be used in view templates, controllers, or services. Some inbuilt filters are as follows.
• date - Format a date to a specified format.
• filter - Select a subset of items from an array.
• Json - Format an object to a JSON string.
• limitTo - Limits an array/string, into a specified number of elements/characters.
• lowercase - Format a string to lowercase.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is view encapsulation in Angular?

A

View encapsulation defines whether the template and styles defined within the component can affect the whole application or vice versa. Angular provides three encapsulation strategies:

  1. Emulated - styles from the main HTML propagate to the component.
  2. Native - styles from the main HTML do not propagate to the component.
  3. None - styles from the component propagate back to the main HTML and therefore are visible to all components on the page.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Explain the lifecycle hooks in Angular

A

In Angular, every component has a lifecycle. Angular creates and renders these components and also destroys them before removing them from the DOM. This is achieved with the help of lifecycle hooks. Here’s the list of them -

  1. ngOnChanges() - Responds when Angular sets/resets data-bound input properties.
  2. ngOnInit() - Initialize the directive/component after Angular first displays the data-bound properties and sets the directive/component’s input properties/
  3. ngDoCheck() - Detect and act upon changes that Angular can’t or won’t detect on its own.
  4. ngAfterContentInit() - Responds after Angular projects external content into the component’s view.
  5. ngAfterContentChecked() - Respond after Angular checks the content projected into the component.
  6. ngAfterViewInit() - Respond after Angular initializes the component’s views and child views.
  7. ngAfterViewChecked() - Respond after Angular checks the component’s views and child views.
  8. ngOnDestroy - Cleanup just before Angular destroys the directive/component.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is String Interpolation in Angular?

A

String Interpolation is a one-way data-binding technique that outputs the data from TypeScript code to HTML view. It is denoted using double curly braces. This template expression helps display the data from the component to the view.
{{ data }}

17
Q
  1. What are Template statements?
A

Template statements are properties or methods used in HTML for responding to user events. With these template statements, the application that you create or are working on, can have the capability to engage users through actions such as submitting forms and displaying dynamic content.
For example,
Delete hero
The template here is deleteHero. The method is called when the user clicks on the button.

18
Q

What is the difference between AOT and JIT?

A

Ahead of Time (AOT) compilation converts your code during the build time before the browser downloads and runs that code. This ensures faster rendering to the browser. To specify AOT compilation, include the –aot option with the ng build or ng serve command.
The Just-in-Time (JIT) compilation process is a way of compiling computer code to machine code during execution or run time. It is also known as dynamic compilation. JIT compilation is the default when you run the ng build or ng serve CLI commands

19
Q

Explain the @Component Decorator.

A

TypeScript class is one that is used to create components. This genre of class is then decorated with the “@Component” decorator. The decorato’s purpose is to accept a metadata object that provides relevant information about the component.

The image above shows an App component - a pure TypeScript class decorated with the “@Component” decorator. The metadata object that gets accepted by the decorator provides properties like templateUrl, selector, and others, where the templateUrL property points to an HTML file defining what you see on the application.

20
Q

What are Services in Angular?

A

Angular Services perform tasks that are used by multiple components. These tasks could be data and image fetching, network connections, and database management among others. They perform all the operational tasks for the components and avoid rewriting of code. A service can be written once and injected into all the components that use that service.

21
Q

What are Promises and Observables in Angular?

A

While both the concepts deal with Asynchronous events in Angular, Promises handle one such event at a time while observables handle a sequence of events over some time.

Promises - They emit a single value at a time. They execute immediately after creation and are not cancellable. They are Push errors to the child promises.

Observables - They are only executed when subscribed to them using the subscribe() method. They emit multiple values over a period of time. They help perform operations like forEach, filter, and retry, among others. They deliver errors to the subscribers. When the unsubscribe() method is called, the listener stops receiving further values.

22
Q

What is ngOnInit? How is it defined?

A
ngOnInit is a lifecycle hook and a callback method that is run by Angular to indicate that a component has been created. It takes no parameters and returns a void type.
export class MyComponent implements OnInit {
  constructor() { }
  ngOnInit(): void {
    //....
  }
}
23
Q

How to use ngFor in a tag?

A

The ngFor directive is used to build lists and tables in the HTML templates. In simple terms, this directive is used to iterate over an array or an object and create a template for each element.

<ul>
<li> {{ item }} </li>
</ul>

  1. “Let item” creates a local variable that will be available in the template
  2. “Of items” indicates that we are iterating over the items iterable.
  3. The * before ngFor creates a parent template
24
Q

What are Template and Reactive forms?

A

Template-driven approach
• In this method, the conventional form tag is used to create forms. Angular automatically interprets and creates a form object representation for the tag.
• Controls can be added to the form using the NGModel tag. Multiple controls can be grouped using the NGControlGroup module.
• A form value can be generated using the “form.value” object. Form data is exported as JSON values when the submit method is called.
• Basic HTML validations can be used to validate the form fields. In the case of custom validations, directives can be used.
• Arguably, this method is the simplest way to create an Angular App.
Reactive Form Approach
• This approach is the programming paradigm oriented around data flows and propagation of change.
• With Reactive forms, the component directly manages the data flows between the form controls and the data models.
• Reactive forms are code-driven, unlike the template-driven approach.
• Reactive forms break from the traditional declarative approach.
• Reactive forms eliminate the anti-pattern of updating the data model via two-way data binding.
• Typically, Reactive form control creation is synchronous and can be unit tested with synchronous programming techniques.

25
Q

What is Eager and Lazy loading?

A

Eager loading is the default module-loading strategy. Feature modules under Eager loading are loaded before the application starts. This is typically used for small size applications.
Lazy loading dynamically loads the feature modules when there’s a demand. This makes the application faster. It is used for bigger applications where all the modules are not required at the start of the application.

26
Q

What type of DOM does Angular implement?

A

DOM (Document Object Model) treats an XML or HTML document as a tree structure in which each node is an object representing a part of the document.
Angular uses the regular DOM. This updates the entire tree structure of HTML tags until it reaches the data to be updated. However, to ensure that the speed and performance are not affected, Angular implements Change Detection.

27
Q

What are Structural Directives?

A

Structural Directives
Structural directives can change the DOM layout by adding and removing DOM elements. . They shape or reshape the DOM’s structure, typically by adding, removing, or manipulating elements. As with other directives, you apply a structural directive to a host element. The directive then does whatever it’s supposed to do with that host element and its descendants. Structural directives are easy to recognize. An asterisk (*) precedes the directive attribute name
*ngIf
*ngSwitch
*ngFor
*ngIf Structural Directive
The *ngIf is an Angular Structural Directive, which allows us to add/remove DOM Element based on some condition.
Syntax:

<p> Welcome </p>

If the expression evaluates to false then the Angular removes the entire element from the DOM. If true, it will insert the element into the DOM.

28
Q

What are Component Directives?

A
Component Directives
Components are special directives in Angular. They are the directive with a template (view)The @Component decorator identifies the class immediately below it as a component class and specifies its metadata.

app.component.ts
selector: Tells the template tag which specifies the beginning and end of the component.
templateURL: Consists of the template used for the component.
styleUrls: It is of array type which consists of all the style format files used for the template.

29
Q

What are Attribute Directives?

A

Attribute Directives
Change the appearance or behavior of an element, component, or another directive. It provides the facility to create our own directive.
Commonly used Attribute directives
ngModel
The ngModel directive is used the achieve the two-way data binding.
Example:

app/component.html

app.component.ts

app.module.ts
ngClass
The ngClass is used to add or remove the CSS classes from an HTML element. Using the ngClass one can create dynamic styles in HTML pages
Example:

app. component.css
app. component.html

app.component.ts
ngStyle
ngStyle is used to change the multiple style properties of our HTML elements. We can also bind these properties to values that can be updated by the user or our components.

30
Q

What type of directive are?
selector: Tells the template tag which specifies the beginning and end of the component.
templateURL: Consists of the template used for the component.
styleUrls: It is of array type which consists of all the style format files used for the template.

A

Component Directive

31
Q

What type of directive is?
ngModel
ngClass
ngStyle

A

Attribute Directive

32
Q

What type of directive is?

  • ngIf
  • ngSwitch
  • ngFor
A

Structural Directive