Initial Value in angular toSignal()

Discussão em 'Angular' iniciado por Burckhardt Sébastien, Janeiro 14, 2025.

  1. I have a problem with a signal, I can't set initialValue:true (TS2769: No overload matches this call.)

    However, the Observable does return an Observable.

    I solved the problem by removing the "initialValue" option and adding "as Signal" but I find this not a satisfactory solution because I don't understand why it doesn't work.

    If you have comments on the use of signals and my code in general, I am also interested :

    export class HeaderComponent implements OnInit {
    private readonly injector = inject(Injector);

    constructor(private scrollDispatcher: ScrollDispatcher,private viewportRuler:ViewportRuler) {}

    ngOnInit(): void {
    this.scrollLimit = toSignal<boolean>(this.scrollDispatcher.scrolled()
    .pipe(
    map(()=> this.viewportRuler.getViewportScrollPosition().top),
    map((top) => top < 349)
    ), {injector: this.injector, intialValue: true}) ;
    }
    }



    here is my unsatisfactory solution which works

    this.scrollLimit = toSignal<boolean>(this.scrollDispatcher.scrolled()
    .pipe(
    startWith(void 0),
    map(()=> this.viewportRuler.getViewportScrollPosition().top),
    map((top) => top < 349)
    ), {injector: this.injector}) as Signal<boolean>;

    Continue reading...

Compartilhe esta Página