How to handed a relationship between two signals by linksignals in Angular v20?

Discussão em 'Angular' iniciado por Juergen, Junho 23, 2025.

  1. Juergen

    Juergen Guest

    I have to mange a relation for X*2=Y, there Y and Y are input fields.

    In the moment, I trigger by a change of the input field the related variable as X changed => Y=X*2 Y changed => X=Y/2

    import { Component, signal } from '@angular/core';
    import { FormsModule } from '@angular/forms';


    @Component({
    selector: 'app-root',
    imports: [FormsModule],

    template: `
    <h1>{{ title }}</h1>
    <input type="number"
    [(ngModel)]="X"
    (change)="FnX()"
    />
    *2 =
    <input type="number"
    [(ngModel)]="Y"
    (change)="FnY()"
    >

    `,

    })
    export class App {
    protected title = 'Relationshiphandling: 2*X=Y'


    X=signal(0);
    Y=signal(0);

    FnX() {
    this.Y.set(2 * this.X());
    };

    FnY() {
    this.X.set(this.Y() / 2);
    };

    }


    Now question is, is it possible to solve such cycles with linkedSignal ? As this AI generated example will not run:

    X: WritableSignal<number> = linkedSignal({
    source: () => this.Y(),
    computation: (_source: number, _prev?: { source: number; value: number }) => {
    const newValue = _source / 2;
    **if (this.X() == newValue) { return 0};** // Do nothing if the same value
    console.log( "this.X.set(newValue)")
    this.X.set(newValue);
    return newValue;
    }
    });


    Y: WritableSignal<number> = linkedSignal({
    source: () => this.X(),
    computation: (_source: number, _prev?: { source: number; value: number }) => {
    const newValue = _source * 2;
    **if (this.Y() == newValue) { return 0};** // Do nothing if the same value

    this.Y.set(newValue)
    console.log( "this.Y.set(newValue)")
    return newValue;
    }
    });

    Continue reading...

Compartilhe esta Página