Skip to content

Commit 79928d0

Browse files
committed
docs: Update README.md to include sections on New Control Flow, Standalone Components, and Angular Signals
1 parent 4beb99a commit 79928d0

File tree

1 file changed

+164
-54
lines changed

1 file changed

+164
-54
lines changed

README.md

+164-54
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ This repository contains a list of resources to learn Angular. It includes tutor
5050
- [Attribute Directives](#attribute-directives)
5151
- [Custom Directives](#custom-directives)
5252
- [Other Directives](#other-directives)
53+
- [New Control Flow](#new-control-flow)
5354
- [Pipes](#pipes)
5455
- [Date Pipe](#date-pipe)
5556
- [Uppercase Pipe](#uppercase-pipe)
@@ -113,6 +114,8 @@ This repository contains a list of resources to learn Angular. It includes tutor
113114
- [Title Service](#title-service)
114115
- [Dynamic Title](#dynamic-title)
115116
- [Meta Service](#meta-service)
117+
- [Standalone Components](#standalone-components)
118+
- [Angular Signals](#angular-signals)
116119
- [Security](#security)
117120
- [Preventing cross-site scripting (XSS)](#preventing-cross-site-scripting-xss)
118121
- [Angular's cross-site scripting security model](#angulars-cross-site-scripting-security-model)
@@ -133,9 +136,6 @@ This repository contains a list of resources to learn Angular. It includes tutor
133136
- [Disabling XSRF protection](#disabling-xsrf-protection)
134137
- [Cross-site script inclusion (XSSI)](#cross-site-script-inclusion-xssi)
135138
- [Auditing Angular applications](#auditing-angular-applications)
136-
- [Standalone Components](#standalone-components)
137-
- [Angular Signals](#angular-signals)
138-
- [Control Flow](#control-flow)
139139
- [Angular Animations](#angular-animations)
140140
- [Installing Angular Animations](#installing-angular-animations)
141141
- [Angular Universal](#angular-universal)
@@ -2012,7 +2012,7 @@ export class AppComponent {
20122012

20132013
[Stackblitz Example](https://stackblitz.com/edit/stackblitz-starters-yerwcu?file=src%2Fmain.ts)
20142014

2015-
## Control Flow
2015+
## New Control Flow
20162016

20172017
Conditionally display content with @if, @else-if and @else
20182018

@@ -5407,6 +5407,166 @@ this.metaService.removeTag("name='robots'");
54075407

54085408
[Back to top⤴️](#table-of-contents)
54095409

5410+
## Standalone Components
5411+
5412+
A standalone component is a type of component which is not part of any Angular module. It provides a simplified way to build Angular applications by reducing the need for NgModules. Standalone components are self-contained and declare their own dependencies.
5413+
5414+
### Creating a Standalone Component
5415+
5416+
```typescript
5417+
import { Component } from '@angular/core';
5418+
import { CommonModule } from '@angular/common';
5419+
5420+
@Component({
5421+
selector: 'app-standalone',
5422+
standalone: true, // Mark as standalone
5423+
imports: [CommonModule], // Import required dependencies
5424+
template: `
5425+
<h1>Standalone Component</h1>
5426+
<p>This is a self-contained component</p>
5427+
`
5428+
})
5429+
export class StandaloneComponent {
5430+
// Component logic here
5431+
}
5432+
```
5433+
5434+
### Key Features of Standalone Components
5435+
5436+
- **Self-contained**: Declares its own dependencies through the imports array
5437+
- **No NgModule required**: Can be used without declaring in a module
5438+
- **Easier testing**: Simpler to test due to explicit dependencies
5439+
- **Better tree-shaking**: Enables more efficient bundle optimization
5440+
- **Simplified lazy-loading**: Can be lazy-loaded directly without module
5441+
5442+
### Using Standalone Components
5443+
5444+
```typescript
5445+
// Importing in another standalone component
5446+
@Component({
5447+
selector: 'app-parent',
5448+
standalone: true,
5449+
imports: [StandaloneComponent],
5450+
template: `
5451+
<app-standalone></app-standalone>
5452+
`
5453+
})
5454+
export class ParentComponent {}
5455+
5456+
// Bootstrapping a standalone component
5457+
import { bootstrapApplication } from '@angular/platform-browser';
5458+
5459+
bootstrapApplication(AppComponent, {
5460+
providers: [
5461+
// Root providers here
5462+
]
5463+
});
5464+
```
5465+
5466+
### Converting to Standalone
5467+
5468+
To convert an existing component to standalone:
5469+
5470+
1. Add `standalone: true` to the component decorator
5471+
2. Move dependencies from NgModule imports to component imports
5472+
3. Remove component declaration from NgModule
5473+
4. Import required dependencies directly in the component
5474+
5475+
[Back to top⤴️](#table-of-contents)
5476+
5477+
## Angular Signals
5478+
5479+
Angular Signals is a powerful system that provides detailed monitoring of state usage within an application, enabling the framework to efficiently optimize rendering updates.
5480+
5481+
### Writing Signals
5482+
5483+
Writable signals provide an API for updating their values directly. You create writable signals by calling the `signal` function with the signal's initial value:
5484+
5485+
```typescript
5486+
import { signal } from '@angular/core';
5487+
5488+
const count = signal(0); // Creates a writable signal with an initial value of 0
5489+
5490+
count.set(5); // Updates the signal's value to 5
5491+
console.log(count()); // Reads the current value of the signal, which is 5
5492+
```
5493+
5494+
### Computed Signals
5495+
5496+
Computed signals are read-only signals that derive their value from other signals. You define computed signals using the `computed` function and specifying a derivation:
5497+
5498+
```typescript
5499+
import { signal, computed } from '@angular/core';
5500+
5501+
const count = signal(0);
5502+
const doubleCount = computed(() => count() * 2); // Creates a computed signal that doubles the count
5503+
5504+
console.log(doubleCount()); // Reads the current value of the computed signal, which is 0
5505+
5506+
count.set(3); // Updates the count signal to 3
5507+
console.log(doubleCount()); // The computed signal automatically updates to 6
5508+
```
5509+
5510+
### Effects
5511+
5512+
Signals are useful because they notify interested consumers when they change. An effect is an operation that runs whenever one or more signal values change. You can create an effect with the `effect` function:
5513+
5514+
```typescript
5515+
import { signal, effect } from '@angular/core';
5516+
5517+
const count = signal(0);
5518+
5519+
effect(() => {
5520+
console.log('Count changed to:', count()); // Logs the count value whenever it changes
5521+
});
5522+
5523+
count.set(5); // Triggers the effect, logging "Count changed to: 5"
5524+
```
5525+
5526+
Here is an example of using Angular Signals in an Angular component:
5527+
5528+
```typescript
5529+
import { Component, OnInit } from '@angular/core';
5530+
import { signal, computed } from '@angular/core'; // Import from '@angular/core'
5531+
5532+
@Component({
5533+
selector: 'my-app',
5534+
templateUrl: './app.component.html',
5535+
styleUrls: ['./app.component.css']
5536+
})
5537+
export class AppComponent implements OnInit {
5538+
count = signal(0);
5539+
doubleCount = computed(() => this.count() * 2);
5540+
5541+
constructor() {}
5542+
5543+
ngOnInit() {
5544+
// Optional logging for debugging displayedCount changes
5545+
// console.log('Displayed count changed to:', this.displayedCount());
5546+
}
5547+
5548+
incrementCount() {
5549+
this.count.set(this.count() + 1);
5550+
}
5551+
5552+
decrementCount() {
5553+
this.count.update((value) => Math.max(0, value - 1));
5554+
}
5555+
}
5556+
```
5557+
5558+
```html
5559+
<h1>Angular Signals Example</h1>
5560+
5561+
<button (click)="incrementCount()" style="margin-right: 10px;">Increment Count</button>
5562+
<button (click)="decrementCount()">Decrement Count</button>
5563+
5564+
<p>Count: {{ count() }}</p>
5565+
<p>Double Count: {{ doubleCount() }}</p>
5566+
```
5567+
5568+
[Back to top⤴️](#table-of-contents)
5569+
54105570
## Security
54115571

54125572
The security of an Angular application is a critical aspect that needs to be considered during development. Here are some best practices to enhance the security of your Angular application:
@@ -5638,56 +5798,6 @@ Auditing Angular applications is an essential step to identify and fix security
56385798

56395799
[Back to top⤴️](#table-of-contents)
56405800

5641-
## Standalone Components
5642-
5643-
A standalone component is a type of component which is not part of any Angular module. It provides a simplified way to build Angular applications.
5644-
5645-
## Angular Signals
5646-
5647-
Angular Signals is a powerful system that provides detailed monitoring of state usage within an application, enabling the framework to efficiently optimize rendering updates.
5648-
5649-
```typescript
5650-
import { Component, OnInit } from '@angular/core';
5651-
import { signal, computed } from '@angular/core'; // Import from '@angular/core'
5652-
5653-
@Component({
5654-
selector: 'my-app',
5655-
templateUrl: './app.component.html',
5656-
styleUrls: ['./app.component.css']
5657-
})
5658-
export class AppComponent implements OnInit {
5659-
count = signal(0);
5660-
doubleCount = computed(() => this.count() * 2);
5661-
5662-
constructor() {}
5663-
5664-
ngOnInit() {
5665-
// Optional logging for debugging displayedCount changes
5666-
// console.log('Displayed count changed to:', this.displayedCount());
5667-
}
5668-
5669-
incrementCount() {
5670-
this.count.set(this.count() + 1);
5671-
}
5672-
5673-
decrementCount() {
5674-
this.count.update((value) => Math.max(0, value - 1));
5675-
}
5676-
}
5677-
```
5678-
5679-
```html
5680-
<h1>Angular Signals Example</h1>
5681-
5682-
<button (click)="incrementCount()" style="margin-right: 10px;">Increment Count</button>
5683-
<button (click)="decrementCount()">Decrement Count</button>
5684-
5685-
<p>Count: {{ count() }}</p>
5686-
<p>Double Count: {{ doubleCount() }}</p>
5687-
```
5688-
5689-
[Back to top⤴️](#table-of-contents)
5690-
56915801
## Angular Animations
56925802

56935803
Angular's animation system is built on CSS functionality in order to animate any property that the browser considers animatable. These properties includes positions, sizes, transforms, colors, borders etc. The Angular modules for animations are @angular/animations and @angular/platform-browser.

0 commit comments

Comments
 (0)