Angular Notes
Angular Notes
RxJS is a JavaScript library that provides methods to manipulate data streams using
observables.
Observable streams are giving developers a rich set of capabilities when creating
Angular applications.
The core of the Angular framework is lightly dependent on RxJS.
Other Angular packages, such as the router and the HTTP client, are more tightly
coupled with observables.
REST stands for REpresentational State Transfer. RESTful applications typically use
lightweight and stateless HTTP calls to read, post (create/update), and delete
data.
A reverse proxy is software responsible for receiving requests and forwarding them
to the appropriate web server. The reverse proxy is exposed directly to the
internet, whereas the underlying web server is exposed only to the proxy. This
setup has several benefits, primarily security and performance for the web servers
Reflection in .NET allows you to obtain information about types in your application
at runtime. You can use reflection to create instances of classes at runtime, and
to invoke and access them
Modules
The main module of our application is a TypeScript file that acts as a container
for the main component.
The component was registered with this module upon creating the Angular
application;
otherwise, the Angular framework would not be able to recognize and load it. A
typical Angular
application has at least a main module called AppModule by convention.
declarations: This contains Angular artifacts that share common functionality bound
to a specific application feature. Artifacts that can be added to this property are
Angular components, directives, and pipes, which we will see in more detail later
in the book. The main module of the application contains the main component by
default.
• imports: This contains other Angular modules whose declarations are needed by the
current module. When an Angular module needs to use features from another module,
it must import it first to start using it. The main application module imports
BrowserModule because it needs its functionality for loading the current
application into the browser.
The imports array should not be confused with the import statements at the top of
the module file. The former is used to import functionality from other Angular
modules into the current module, whereas the latter is for importing their
respective JavaScript modules.
bootstrap: Defines the component that will be loaded at application startup. The
bootstrap property is set only once in the main application module and is usually
the main component. You should not change it unless there is a compelling reason.
The AppModule TypeScript class is empty because Angular modules usually do not
contain any logic. As we have learned, the primary purpose of an Angular module is
to group artifacts with similar functionality. Their purpose is fulfilled by using
the @NgModule decorator above the class.
The Angular framework would treat AppModule as a single TypeScript class if the
decorator was
missing. It actually tells Angular that this is indeed an Angular module.
###
The import statement at the top of the file is used to import the Component
artifact from the @angular/core npm package.
The Component artifact is an Angular decorator that is used to configure an Angular
component.
The TypeScript file of the component also has a TypeScript class called
AppComponent that contains a title property.
The @Component decorator above the class tells Angular that it is an Angular
component.
If the decorator was missing, the Angular framework would treat it as a simple
TypeScript class.
component with the products module by adding the ProductListComponent class in the
declarations array of the products.module.ts file:
@NgModule({
declarations: [
ProductListComponent
],
imports: [
CommonModule
]
})
export class ProductsModule { }
-----------------
To display and interact with the template through the following topics:
• Loading the component template
• Displaying data from the component class
• Styling the component
• Getting data from the template
----------
Directives:
Angular directives are HTML attributes that extend the behavior or the appearance
of a standard HTML element.
When we apply a directive to an HTML element or even an Angular component, we can
add custom behavior to it or alter its appearance.
• BrowserModule: This is used to run Angular applications in the browser and must
be
imported only once in an Angular application.
• CommonModule: This contains specific Angular artifacts that support the Angular
template
syntax and enrich our HTML templates. Typical examples include directives for
looping or
displaying HTML content conditionally and applying CSS styles in HTML.
The preceding list contains Angular modules you will primarily use in an Angular
application as
you start out. The Angular framework contains a lot more for many business needs
and use cases.
Pipes allow us to digest and transform the information we bind in our templates.
Directives allow us to conduct more ambitious functionalities, such as manipulating
the DOM or altering the appearance and behavior of HTML elements.
we can create Angular pipes that are not registered with an Angular module
and are standalone. To create a standalone pipe using the Angular CLI, we pass the
standalone
option in the generate command as follows:
Introducing directives
Angular directives are HTML attributes that extend the behavior or the appearance
of a standard
HTML element. When we apply a directive to an HTML element or even an Angular
component,
we can add custom behavior to it or alter its appearance.
Angular provides us with a set of built-in directives that we can use in our
components to cover
most use cases. Angular built-in directives are part of the CommonModule. So, we
need to import
CommonModule when we want to use them.
We can create an interceptor using the generate command of the Angular CLI. The
following
command will create an Angular interceptor named auth:
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule,
ProductsModule,
AuthModule
],
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true }
],
bootstrap: [AppComponent]
})
An HTTP interceptor must be provided in the same Angular module that imports
HttpClientModule.
-----------------
In Angular, services and standalone components serve different purposes and have
distinct roles within an application. Here are the key differences between services
and standalone components:
In Angular, template expressions and template statements are two distinct ways of
interacting with data and performing actions within the template.
1. Template Expressions:
Template expressions are enclosed in double curly braces ({{ }}) and are used to
display dynamic values or perform simple calculations within the template. They are
evaluated by Angular and can access properties and methods of the component class.
Example:
```html
<p>{{ username }}</p>
```
In this example, the expression `{{ username }}` will be replaced with the value of
the `username` property from the component class.
2. Template Statements:
Template statements allow you to respond to events and perform more complex actions
within the template. They use event binding and are enclosed in parentheses
followed by the event name, such as `(click)="handleClick()"`.
Example:
```html
<button (click)="handleClick()">Click me</button>
```
In this example, the `(click)` statement is bound to the `handleClick()` method in
the component class. When the button is clicked, the method will be executed.
Overall, template expressions are mainly used for data interpolation and display
purposes, while template statements are used for event handling and executing
actions within the template.
---------------------------
Rest and spread are two concepts in JavaScript that involve manipulating arrays or
objects.
Rest:
Rest parameters are used to represent an indefinite number of arguments as an
array. It allows you to gather multiple function arguments into a single array.
Rest parameters are denoted by three dots (...) followed by a parameter name in a
function declaration.
For example:
```javascript
function sum(...numbers) {
let total = 0;
for (let number of numbers) {
total += number;
}
return total;
}
In the above example, the rest parameter `...numbers` gathers all the arguments
passed to the `sum` function and stores them in an array called `numbers`. This
allows you to pass any number of arguments to the function without explicitly
defining them as separate parameters.
Spread:
Spread syntax is used to expand an array or object into individual elements. It
allows you to clone or merge arrays and objects easily. Spread syntax is denoted by
three dots (...) followed by the name of the array or object you want to spread.
For example:
```javascript
const numbers = [1, 2, 3];
const newArray = [...numbers, 4, 5];
In the above example, the spread syntax `...numbers` expands the `numbers` array
into individual elements, which are then combined with `4` and `5` to create a new
array called `newArray`.
Similarly, spread syntax can be used to clone objects or merge multiple objects
into one:
```javascript
const obj1 = { name: 'John' };
const obj2 = { age: 25 };
In this case, the spread syntax `...obj1` and `...obj2` expands the properties of
`obj1` and `obj2` into a new object called `mergedObj`, effectively merging the
properties of both objects.
In summary, rest parameters allow you to gather multiple function arguments into an
array, while spread syntax allows you to expand arrays or objects into individual
elements for cloning or merging purposes.
-------------
call, apply, bind
`call`, `apply`, and `bind` are methods in JavaScript that are used to manipulate
the execution context of a function.
1. `call`:
The `call` method is used to invoke a function with a specified context (`this`
value) and arguments provided individually.
Syntax: `function.call(thisArg, arg1, arg2, ...)`
For example:
```javascript
function greet(name) {
console.log(`Hello, ${name}! My name is ${this.name}.`);
}
const person = {
name: 'John'
};
greet.call(person, 'Alice');
// Output: Hello, Alice! My name is John.
```
In the above example, `call` is used to invoke the `greet` function with the
`person` object as the context (`this` value). The additional argument `'Alice'` is
passed as a separate argument.
2. `apply`:
The `apply` method is similar to `call`, but it accepts the function arguments as
an array or an array-like object.
Syntax: `function.apply(thisArg, [argsArray])`
For example:
```javascript
function sum(a, b, c) {
return a + b + c;
}
In the above example, `apply` is used to invoke the `sum` function with the
`numbers` array as arguments. The `null` value is passed as the context (`this`
value) since the `sum` function does not rely on the context.
3. `bind`:
The `bind` method creates a new function with a specified context (`this` value)
and partially applied arguments. It allows you to create a new function that, when
called, will have a fixed context and, optionally, some initial arguments.
Syntax: `function.bind(thisArg, arg1, arg2, ...)`
For example:
```javascript
function multiply(a, b) {
return a * b;
}
In the above example, `bind` is used to create a new function `multiplyByTwo` that
has the context (`this` value) set to `null` and the first argument fixed as `2`.
When `multiplyByTwo` is called with a single argument `5`, it multiplies `2` and
`5` to produce the result `10`.
The main difference between `call`, `apply`, and `bind` is in how they handle
function invocation and argument passing. `call` and `apply` immediately invoke the
function, while `bind` returns a new function with the desired context and pre-set
arguments, allowing it to be invoked later.
---------------------------