Questions I Flashcards
What is data binding? Which type of data binding does Angular deploy?
Angular uses the two-way binding. Any changes made to the user interface are reflected in the corresponding model state
Conversely, any changes in the model state are reflected in the UI state. This allows the framework to connect the DOM to the Model data via the controller.
However, this approach affects performance since every change in the DOM has to be tracked.
What are Single Page Applications (SPA)?
Single-page applications are web applications that load once with new features just being mere additions to the user interface.
It does not load new HTML pages to display the new page’s content, instead generated dynamically. This is made possible through JavaScript’s ability to manipulate the DOM elements on the existing page itself. A SPA approach is faster, thus providing a seamless user experience.
What are decorators in Angular?
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:
Class Decorators
Property Decorators
Method Decorators
Parameter Decorators
Library vs framework
Library as a tool you use to do a specific job, like a hammer or a screwdriver. A framework, on the other hand, is more like a set of blueprints for building a house
What are Directives in Angular?
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.
Component Directives
-ngClass
-ngStyle
-ngModel
Structural Directives
Attribute Directives
What is an AOT compilation? What are its advantages?
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.
Faster rendering
Fewer asynchronous requests
Smaller Angular framework download size
Quick detection of template errors
Better security
What are Pipes in Angular?
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:
- Pipes are defined using the pipe “|” symbol.
- Pipes can be chained with other pipes.
- Pipes can be provided with arguments by using the colon (:) sign.
What are Pure Pipes?
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.
What are Impure Pipes?
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.
What is an ngModule?
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.
What are filters in Angular? Name a few of them.
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.
Explain the lifecycle hooks in Angular
ngOnChanges() - Responds when Angular sets/resets data-bound input properties.
ngOnInit() - Initialize the directive/component after Angular first displays the data-bound properties and sets the directive/component’s input properties/
ngDoCheck() - Detect and act upon changes that Angular can’t or won’t detect on its own.
ngAfterContentInit() - Responds after Angular projects external content into the component’s view.
ngAfterContentChecked() - Respond after Angular checks the content projected into the component.
ngAfterViewInit() - Respond after Angular initializes the component’s views and child views.
ngAfterViewChecked() - Respond after Angular checks the component’s views and child views.
ngOnDestroy - Cleanup just before Angular destroys the directive/component.
What is String Interpolation in Angular?
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 }}
What is the difference between AOT and JIT?
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.
Explain the @Component Decorator.
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.
-selector
-template
-style
What are Services in Angular?
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.
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.