|
46 | 46 | - [OnDestroy](#ondestroy) |
47 | 47 | - [Forms](#forms) |
48 | 48 | - [Signals](#signals) |
| 49 | +- [Angular Signals](#angular-signals) |
| 50 | +- [Destroy Ref](#destroy-ref) |
49 | 51 | - [Http](#http) |
50 | 52 | - [Module](#module) |
51 | 53 | - [Router](#router) |
@@ -1560,7 +1562,77 @@ FormArrays |
1560 | 1562 |
|
1561 | 1563 | ## Signals |
1562 | 1564 |
|
1563 | | -A signal is a wrapper around a value that can notify interested consumers when that value changes. Signals can contain any value, from simple primitives to complex data structures. |
| 1565 | +Signals serve as wrappers around values, offering the ability to notify interested consumers whenever the encapsulated value undergoes a change. These signals can accommodate a wide range of values, encompassing both basic primitives and intricate data structures. |
| 1566 | +
|
| 1567 | +## Angular Signals |
| 1568 | +
|
| 1569 | +Angular Signals is a powerful system that provides detailed monitoring of state usage within an application, enabling the framework to efficiently optimize rendering updates. |
| 1570 | +
|
| 1571 | +```typescript |
| 1572 | +import { Component, OnInit } from '@angular/core'; |
| 1573 | +import { signal } from '@angular/cdk/platform-browser'; |
| 1574 | + |
| 1575 | + @Component({ |
| 1576 | + selector: 'my-app', |
| 1577 | + templateUrl: './app.component.html', |
| 1578 | + styleUrls: ['./app.component.css'] |
| 1579 | +}) |
| 1580 | +export class AppComponent implements OnInit { |
| 1581 | + |
| 1582 | +count = signal(0); |
| 1583 | + |
| 1584 | +constructor() { } |
| 1585 | + |
| 1586 | +ngOnInit() { |
| 1587 | + this.count.subscribe(value => { |
| 1588 | + console.log('Count changed to', value); |
| 1589 | + }); |
| 1590 | +} |
| 1591 | + |
| 1592 | +incrementCount() { |
| 1593 | + this.count.value++; |
| 1594 | +} |
| 1595 | + |
| 1596 | +} |
| 1597 | +``` |
| 1598 | +
|
| 1599 | +```html |
| 1600 | +<h1>Angular Signals Example</h1> |
| 1601 | + |
| 1602 | +<button (click)="incrementCount()">Increment Count</button> |
| 1603 | + |
| 1604 | +<p>Count: {{ count }}</p> |
| 1605 | +``` |
| 1606 | +
|
| 1607 | +## Destroy Ref |
| 1608 | +
|
| 1609 | +DestroyRef is a new provider in Angular 16 that allows you to register destroy callbacks for a specific lifecycle scope. This feature is applicable to components, directives, pipes, embedded views, and instances of EnvironmentInjector. |
| 1610 | +
|
| 1611 | +```typescript |
| 1612 | +import { Component, OnInit, OnDestroy } from '@angular/core'; |
| 1613 | +import { DestroyRef } from '@angular/core/testing'; |
| 1614 | + |
| 1615 | +@Component({ |
| 1616 | + selector: 'my-component', |
| 1617 | + templateUrl: './my-component.component.html', |
| 1618 | + styleUrls: ['./my-component.component.css'] |
| 1619 | +}) |
| 1620 | +export class MyComponent implements OnInit, OnDestroy { |
| 1621 | + |
| 1622 | + constructor(private destroyRef: DestroyRef) {} |
| 1623 | + |
| 1624 | + ngOnInit() { |
| 1625 | + } |
| 1626 | + |
| 1627 | + ngOnDestroy() { |
| 1628 | + // Register a destroy callback with the DestroyRef provider. |
| 1629 | + this.destroyRef.register(() => { |
| 1630 | + // Do any cleanup tasks here. |
| 1631 | + }); |
| 1632 | + } |
| 1633 | + |
| 1634 | +} |
| 1635 | +``` |
1564 | 1636 |
|
1565 | 1637 | ## HTTP |
1566 | 1638 |
|
|
0 commit comments