Standalone & Tokens

Preview — 3 of 10 questions

What is runInInjectionContext used for?

javascript
import { runInInjectionContext, EnvironmentInjector, inject } from '@angular/core';

@Injectable({ providedIn: 'root' })
class DynamicFeatureLoader {
  constructor(private envInjector: EnvironmentInjector) {}

  async loadFeature(featureName: string) {
    const module = await import(`./features/${featureName}`);

    return runInInjectionContext(this.envInjector, () => {
      // inject() is valid here
      const config = inject(APP_CONFIG);
      const logger = inject(Logger);
      return module.initialize(config, logger);
    });
  }
}
ABypassing injection token resolution for testing
BCreating a child injector with additional providers
CRunning code that calls inject() outside the natural DI construction phase
DRunning code in the NgZone to trigger change detection

When would you use createEnvironmentInjector instead of runInInjectionContext?

javascript
import { createEnvironmentInjector, EnvironmentInjector } from '@angular/core';

@Injectable({ providedIn: 'root' })
class FeatureManager {
  private featureInjectors = new Map<string, EnvironmentInjector>();

  constructor(private rootInjector: EnvironmentInjector) {}

  getFeatureInjector(feature: string, providers: Provider[]): EnvironmentInjector {
    if (!this.featureInjectors.has(feature)) {
      const injector = createEnvironmentInjector(
        providers,
        this.rootInjector,
        `Feature:${feature}`   // debug name
      );
      this.featureInjectors.set(feature, injector);
    }
    return this.featureInjectors.get(feature)!;
  }

  cleanup(feature: string) {
    this.featureInjectors.get(feature)?.destroy();
    this.featureInjectors.delete(feature);
  }
}
AWhen you need to run async operations with injection
BWhen you need a persistent child injector with its own provider scope (not just a temporary context)
CWhen you want to create component-level injectors
DWhen testing services that have no constructor parameters

How does Angular handle and how should you resolve circular service dependencies?

javascript
// ❌ Circular: UserService injects AuthService, AuthService injects UserService
@Injectable({ providedIn: 'root' })
class UserService { constructor(private auth: AuthService) {} }

@Injectable({ providedIn: 'root' })
class AuthService { constructor(private user: UserService) {} }  // circular!

// ✅ Fix 1: Extract shared logic into a third service
@Injectable({ providedIn: 'root' })
class SessionService { /* shared state */ }

@Injectable({ providedIn: 'root' })
class UserService { constructor(private session: SessionService) {} }

@Injectable({ providedIn: 'root' })
class AuthService { constructor(private session: SessionService) {} }

// ✅ Fix 2: forwardRef for class-declaration ordering issues
@Injectable({ providedIn: 'root' })
class A { constructor(@Inject(forwardRef(() => B)) private b: B) {} }
AAngular throws a runtime error; resolve by extracting the shared logic into a third service, or use forwardRef() for class references
BAngular resolves them using a two-pass instantiation algorithm — no code change needed
CUse @Optional() on one side of the circular reference
DAngular automatically breaks cycles using lazy initialization

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.