Get Updated List using Angular Signal
Last Updated :
09 May, 2024
Angular Signal is a new feature introduced in Angular 16 that provides a way to handle state management and reactive data flow within Angular applications. It offers a simpler and more efficient alternative to traditional state management solutions like RxJS Observables and NgRx.
Prerequisites
What are signals in angular?
Signals are wrappers around value, that notify all the consumers when the value gets updated. It can contain any value, from primitive to user-defined data structure. Its value is always read through a getter function, which helps angular to remember where it is being used.
What are writable signals?
The writable signals provide an API to update their value directly. When we update any value, it notifies all the consumers that a value has been changed.
Steps to Create Angular Application
Step 1: Create an angular application
We can make use of the angular cli to create a new application.
ng new writable-signals
cd writable-signals
This will create a new app in the directory writable-signals, we then use cd command to change active directory.
Folder Structure
Folder Struture
Dependencies
"dependencies": {
"@angular/animations": "^17.3.0",
"@angular/common": "^17.3.0",
"@angular/compiler": "^17.3.0",
"@angular/core": "^17.3.0",
"@angular/forms": "^17.3.0",
"@angular/platform-browser": "^17.3.0",
"@angular/platform-browser-dynamic": "^17.3.0",
"@angular/router": "^17.3.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.3"
}
Step 2: Create a signal array
We can create a simple array of random numbers wrapped with the signal wrapper and display it in the template. Its value can be read by calling it like a function.
HTML
<!-- app.component.html -->
<div class="container">
@for (val of myArr(); track $index) {
<span class="value">
{{val}}
</span>
}
</div>
<button type="button" (click)="updateAr()">
Update
</button>
CSS
/* app.component.scss */
.container {
max-width: 850px;
padding: 16px;
margin: 32px auto;
display: flex;
flex-direction: row;
flex-wrap: wrap;
align-items: center;
gap: 16px;
}
button {
display: block;
margin: 0 auto;
}
JavaScript
// app.component.ts
import { Component, WritableSignal, signal } from '@angular/core';
import { RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet],
templateUrl: './app.component.html',
styleUrl: './app.component.scss',
})
export class AppComponent {
myArr: WritableSignal<Array<number>>;
constructor() {
this.myArr = signal(this.getRandomArr());
}
getRandomArr() {
return Array.from({ length: 10 },
() => Math.round(Math.random() * 100));
}
updateAr() {
this.myArr.update((oldValue) => {
console.log('oldValue', oldValue);
return this.getRandomArr();
});
}
}
Output
Get Updated List using Angular Signal
Similar Reads
Signals in Angular Signals are a reactive data model that provides a simple way to manage and track changes in state within an application. A Signal in Angular is a variable whose value can change over time and any change to this value will automatically propagate to the parts of the application that depend on it.Key
5 min read
<mat-list> in Angular Material Introduction:Angular Material is a UI component library that is developed by the Angular team to build design components for desktop and mobile web applications. In order to install it, we need to have angular installed in our project, once you have it you can enter the below command and can downloa
2 min read
Angular PrimeNG OrderList Templates Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will learn about Angular PrimeNG OrderList Templates. The Orderlist component is
3 min read
How to fetch the details using ng-repeat in AngularJS ? In this article, we will see how to fetch the details with the help of the ng-repeat directive in Angular, along with understanding its implementation through the illustrations. AngularJS contains various types of pre-defined Directives, where most of the directives start with ng which denotes Angul
2 min read
How to fetch the details using ng-repeat in AngularJS ? In this article, we will see how to fetch the details with the help of the ng-repeat directive in Angular, along with understanding its implementation through the illustrations. AngularJS contains various types of pre-defined Directives, where most of the directives start with ng which denotes Angul
2 min read
Angular Material List Angular Material is a UI component library developed by Google so that Angular developers can develop modern applications in a structured and responsive way. By making use of this library, we can greatly increase the user experience of an end-user thereby gaining popularity for our application. This
4 min read