Why is does angular require modular division for a resetting counter?

Discussão em 'Angular' iniciado por earnshae, Março 9, 2025.

  1. earnshae

    earnshae Guest

    I am looking for an explanation for the following behaviour.

    So I am doing a tutorial located here.

    The tutorial asks you to make a counter component which counts to 25 if the number is greater then 25 and then resets to 1 via setter function.

    I thought the setter should be written like this:

    set counter(val: number) {
    this._counter = val;
    if(this._counter > 25) this.counter = 1;
    }
    private _counter = 1;


    AppComponent is implemented as follows:

    export class AppComponent {
    title = 'sample-app';
    counter = 10;
    inc() {
    this.counter++;
    }
    }


    This doesn't work it counts up to 25 and then stays at 1.

    The code from the tutorial does work however:

    set counter(val: number) {

    this._counter = val || 0;
    if (val > 25) this._counter = val % 25;


    }

    private _counter: number = 1;

    That doesn't look right to me... as I can imagine values that would not assign 1 to the counter.

    Continue reading...

Compartilhe esta Página