# Review No. 1

In 
Published 2024-01-06

This page contains questions to improve/test your Angular skills.

# What is Angular?

Angular is a TypeScript-based open-source web application framework developed by Google for building dynamic, single-page web applications (SPAs).

# What are the key features of Angular ?

  • Two-way data binding
  • Dependency injection
  • Modular architecture with components
  • Services
  • Directives
  • Templates

# What is Two-Way Data Binding in Angular ?

Two-way data binding is a synchronization mechanism between the model and the view. Changes in the model update the view, and changes in the view update the model automatically.

# What is Dependency Injection in Angular ?

Dependency Injection is a design pattern in which a class's dependencies are provided from the outside. Angular uses a hierarchical injector to manage the dependency injection.

# What is Angular CLI ?

Angular CLI (Command Line Interface) is a command-line tool used for initializing, developing, and maintaining Angular applications. It helps automate various development tasks.

# What are the Modules in Angular ?

Modules in Angular are containers for components, directives, and pipes. They help in:

  • organizing better the big applications
  • load the modules when we need it (lazy & eager loading)

# Which are the Angular module types ?

The types of modules available in Angular include:

  • Root modules : used to bootstrap the application
  • Core modules : are part of the Angular core and provide essential functionality
  • Routing modules : for mapping the components to URLs
  • Shared modules : modules which can be used by Feature modules
  • Feature modules : the modules which bring the application functionalities

# What is a Component in Angular?

A component is a basic building block of an Angular application. It represents a part of the user interface (UI) and encapsulates the logic and data for that portion of the application.

A Component teaches the browser new tags, and it is defined in a ts file.

# What is the purpose of the ngOnInit lifecycle hook?

The ngOnInit lifecycle hook is used for initialization logic in a component. It is called after Angular has initialized all data-bound properties of a component.

# What are Angular Directives?

Directives are instructions in the DOM (Document Object Model) that tell Angular to modify a DOM element.

# What are the Custom Directives in Angular ?

In Angular, custom directives are used to extend the functionality of HTML elements. You can create custom directives to encapsulate and reuse behavior throughout your application.

# What kind of directives we have in Angular ?

In Angular we have 2 types of directives :

  • structural directives : NgIf, NgFor, NgSwitch
  • non-structural directives : NgClass, NgStyle, NgModel

# What is a Service in Angular?

A Service in Angular is singleton object that is used to organize and share code across the application. The services are commonly used for tasks such as manipulate data, sharing state, or performing common functionality.

# What is Lazy Loading in Angular?

Lazy Loading is a technique in Angular where modules are loaded on-demand (when they are required). This helps in reducing the initial loading time of the application.

# What are the Pipes in Angular ?

In Angular, pipes are a way to transform and format data in the template or in the .ts file. In the ts file, we can use the transform() method.

# Which are the principal commands in Angular CLI ?

  • ng test : runs the unit tests in the project
  • ng build : build the application in dist folder. We can specify the base href using --base-href option, or we can build the application using --aot (Ahead Of Time) option.
  • ng upgrade : for upgrading the application
  • ng serve : build and run the application. We can use the following options: --allowed_hosts, --disable-host-check, --port, --host.

# Which is the entry point in an Angular Application ?

We can consider angular.json (configuration file) as entry point. In this file we have the location of the main.ts file which points to the bootstrap module (which includes the bootstrap component).

# Which is the meaning of "export" keyword ?

In TypeScript, the export keyword is used to make a class, function, variable available for use in other files or modules. When a class is marked with export, it means that the class can be imported and used in other files.

# What is NgModule decorator used for ?

In Angular, NgModule (NgModule stands for "Angular Module") is a decorator that is used to define and configure modules.

@NgModule({
  declarations: [AppComponent, MyComponent, MyDirective, MyPipe],
  imports: [RouterModule, HttpClientModule],
  providers: [MyService],
  bootstrap: [AppComponent],
  entryComponents: [DynamicComponent],
  exports: [MyComponent],
})
  • declarations : declares the components, directives, and pipes that belong to that module. These declared elements are then available for use within the module.
  • imports : import other modules to reuse their functionality.
  • providers : to specify the services that are provided at the module level. This ensures that a single instance of the service is shared across the components and services within that module.
  • bootstrap : to specify the main component of the module that should be bootstrapped when the application starts.
  • entryComponents : to specify components that are not referenced in the templates but are created dynamically, such as through dynamic component loading or Angular's ViewContainerRef.createComponent method.
  • exports : to make certain components, directives, or pipes from the module available for use in other modules that import it.

# What are the Observables in Angular ?

The Observables are objects which emits streams of values.

# Which are the Observables types ?

  • HOT observables : you receive all data when you subscribe to it
  • COLD observables : you receive data from it continuously

# What is an Observer ?

An Observer is an object which subscribes to an Observable and do something "on next", "on error" and "on complete".

# What is a Subject ?

A Subject is an object which is an Observable and an Observer in the same time. A Subject can see only the NEW data from the subscription time.

# What is a BehaviourSubject ?

A BehaviourSubject is a Subject which can see the INITIAL/LAST value and the NEW data from the subscription time.

# What is a ReplaySubject ?

A BehaviourSubject is a Subject which can see the LAST values and the NEW data from the subscription time.

# What is an AsyncSubject ?

A AsyncSubject is a Subject which can see the LAST value of the stream when the stream completes.

# Which are the most important operators on Angular streams ?

  • filter(value => value % 2 === 0) : filter the values from the stream

  • tap(value => console.log(`Value: ${value}`)) : used for seeing the values in the stream

  • map(value -> value+1) : used for modifying the values in the stream

  • map((value,index) -> value+index) : used for modifying the values in the stream

  • scan((acc, value) => acc + value, 0) : return stream values based on a logic

  • skip(3) : the first 3 values are skipped

  • take(3) : we take only the first 3 values from the stream

  • takeWhile(val => val < 3) : when the condition is not met the stream will stop

  • skipWhile : acts as filter, but its behaviour is opposite

  • of(5,6,7).concatMap((val, i)) => Observable.Of(0,1,val)) => 0,1,5, 0,1,6, 0,1,7

  • reduce((acc, value) => acc + value, 0) : return the last value of logic defined inside operator

# What is a callback ?

A Callback is a function received as parameter by another function, and it is intended to be executed at a later point in time or in response to a specific event.

# How can we manually inject a dependency ?

Before Angular 14

export class MyComponent {
    public loadingService;
    
    constructor() {
        this.loadingService = ServiceLocator.injector.get(LoadingService)
    }

    showLoading() {
        this.loadingService.show();
    }
}

After Angular 14

export class MyComponent {
    public loadingService;
    
    constructor() {
        this.loadingService = inject(LoadingService)
    }

    showLoading() {
        this.loadingService.show();
    }
}