use effect and afterNextRender together

Discussão em 'Angular' iniciado por Robert, Novembro 21, 2025.

  1. Robert

    Robert Guest

    I have a signal effect that I want to run only in the browser (and nothing in the server)

    but both effect and afterNextRender require being run from the injection context

    so running this code

    effect(() => {
    afterNextRender(() => {
    // ...
    });
    });


    throw ERROR Error: NG0203: afterNextRender() can only be used within an injection context such as a constructor, a factory function, a field initializer, or a function used with `runInInjectionContext`. Find more at https://angular.io/errors/NG0203

    and running

    afterNextRender(() => {
    effect(() => {
    // ...
    });
    });


    throw ERROR Error: NG0203: effect() can only be used within an injection context such as a constructor, a factory function, a field initializer, or a function used with `runInInjectionContext`. Find more at https://angular.io/errors/NG0203

    Continue reading...

Compartilhe esta Página