Skip to content

Instantly share code, notes, and snippets.

View Purecaesar's full-sized avatar
🎾

Georgy Purecaesar

🎾
View GitHub Profile
import { Injectable, Injector } from "@angular/core";
@Injectable()
export class StaticInjectorService {
static Injector: Injector
constructor(injector: Injector) {
StaticInjectorService.Injector = injector;
}
}
import { Injectable, Injector } from "@angular/core";
@Injectable()
export class StaticInjectorService {
static Injector: Injector
constructor(injector: Injector) {
StaticInjectorService.Injector = injector;
}
}
import { NgModule } from "@angular/core";
import { StaticInjectorService } from "./services/static-injector.service";
@NgModule({
providers: [StaticInjectorService],
})
export class NgResolveModule {
constructor(_: StaticInjectorService) {}
}
export function NgResolve(name?: string, propagation = true) {
return function(
target: any,
key: string,
originalDescriptor?: TypedPropertyDescriptor<any>
): any {
const router = StaticInjectorService.Injector.get(Router);
const triger = new Subject();
const destroyer = new Subject();
const rootContextMap = (router as any).rootContexts.contexts as Map<
export function NgResolve(name?: string, propagation = true) {
return function(
target: any,
key: string,
originalDescriptor?: TypedPropertyDescriptor<any>
): any {
const router = StaticInjectorService.Injector.get(Router);
const triger = new Subject();
const destroyer = new Subject();
const rootContextMap = (router as any).rootContexts.contexts as Map<
triger
.pipe(
switchMap(() => {
const currentActivatedOutlet = getCurrentOutlet(
rootContextMap,
target.constructor
);
if (!currentActivatedOutlet)
throw new Error('Component is not in router tree');
import { OutletContext } from "@angular/router";
export function getCurrentOutlet(contextsMap: Map<string, OutletContext>, component: any) {
const contextsArray = Array.from(contextsMap.values());
while (contextsArray.length) {
const outlet = contextsArray.shift();
if (outlet.route?.component === component) {
return outlet;
import { ActivatedRoute } from "@angular/router";
export function getRouteWithData(route: ActivatedRoute, fieldName: string, propagation: boolean): ActivatedRoute {
do {
if (fieldName in route.snapshot.data) return route;
route = route.parent;
} while (route && propagation);
if (propagation) throw new Error(`Field ${fieldName} didn't find in route tree`);
...
cdr = compRef.injector.get(ChangeDetectorRef);
compRef.onDestroy(() => {
inited = false;
destroyer.next();
});
return routeWithData.data.pipe(
pluck(name || key),
...
.subscribe({
next: data => {
target[key] = data;
cdr.markForCheck();
}
});
...