Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to handle / bind Angular signals with <input> of the html?

In my Angular app the variable value will be changed by a HTML element <input> and two-way binding as

<input
    [(ngModel)]=variableName
    (OnKeyup)="DoSomething"
  >

But now [(ngModel)] will not work for signals.
Should it now the best praxis to use e.g. a (onKeyup) to start a function, which will excecute … variableName.set(value).
And how to get the value and "sent" back to the input field value?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is the equivalent of ngModel in signals syntax!

import { Component, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { bootstrapApplication } from '@angular/platform-browser';
import 'zone.js';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [FormsModule],
  template: `
  <input type="text"
        [ngModel]="quantity()"
        (input)="onQuantitySelected($any($event.target).value)"/>

        {{quantity()}}
  `,
})
export class App {
  name = 'Angular';
  quantity = signal<number>(1);

  onQuantitySelected(qty: number) {
    this.quantity.set(qty);
  }
}

bootstrapApplication(App);

stackblitz


References:

medium article

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading