GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Docs
  • Tutorials
  • Playground
  • Reference
  • code

This view

  • Original page
  • New address

GeezerVison is showing angular.dev — forwardRef • Angular

API

function forwardRef(forwardRefFn: ForwardRefFn): Type<any>;
@paramforwardRefFnForwardRefFn
@returnsType<any>

Description

Allows to refer to references which are not yet defined.

For instance, forwardRef is used when the token which we need to refer to for the purposes of DI is declared, but not yet defined. It is also used when the token which we use when creating a query is not yet defined.

forwardRef is also used to break circularities in standalone components imports.

Usage Notes

Circular dependency example

@Injectable()
      class Door {
        lock: Lock;

        // Door attempts to inject Lock, despite it not being defined yet.
        // forwardRef makes this possible.
        constructor(@Inject(forwardRef(() => Lock)) lock: Lock) {
          this.lock = lock;
        }
      }

      // Only at this point Lock is defined.
      class Lock {}

      const injector = Injector.create({
        providers: [
          {provide: Lock, deps: []},
          {provide: Door, deps: [Lock]},
        ],
      });

      expect(injector.get(Door) instanceof Door).toBe(true);
      expect(injector.get(Door).lock instanceof Lock).toBe(true);

Circular standalone reference import example

@Component({
  imports: [ChildComponent],
  selector: 'app-parent',
  template: `<app-child [hideParent]="hideParent()"/>`,
})
export class ParentComponent {
   hideParent = input.required<boolean>();
}


@Component({
  imports: [forwardRef(() => ParentComponent)],
  selector: 'app-child',
  template: `
   @if(!hideParent()) {
      <app-parent/>
   }
 `,
})
export class ChildComponent {
   hideParent = input.required<boolean>();
}
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.