Search code examples
angularvariablessignals

Angular Signals - problems when using variables


I am modifying all the variables in my angular project to be able to use Signals. In general I get it to work, but when I try to modify a variable that I later use in an ngOnInit, constructor or other function I can't get it to work.

Here is an example:

I create my variable

currentYear = signal<number>(0);

And when I want to use it, it tells me that Type 'number' is not assignable to type 'WritableSignal'.

ngOnInit() {

    this.innerHeight = window.innerHeight;

    this.currentYear = moment().year();

    const currentDate = new Date();
    this.rangeDate[0] = currentDate;
    this.rangeDate[1] = moment(currentDate)
      .add(1, 'month')
      .startOf('month')
      .endOf('month')
      .toDate();
  }

How can I use my variables?

Thank you very much


Solution

  •     this.currentYear = moment().year();
    

    Should be

        this.currentYear.set(moment().year());
    

    This is how signals work. Pay better attention to the documentation !