This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable, Injector } from "@angular/core"; | |
@Injectable() | |
export class StaticInjectorService { | |
static Injector: Injector | |
constructor(injector: Injector) { | |
StaticInjectorService.Injector = injector; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable, Injector } from "@angular/core"; | |
@Injectable() | |
export class StaticInjectorService { | |
static Injector: Injector | |
constructor(injector: Injector) { | |
StaticInjectorService.Injector = injector; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NgModule } from "@angular/core"; | |
import { StaticInjectorService } from "./services/static-injector.service"; | |
@NgModule({ | |
providers: [StaticInjectorService], | |
}) | |
export class NgResolveModule { | |
constructor(_: StaticInjectorService) {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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< |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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< |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
triger | |
.pipe( | |
switchMap(() => { | |
const currentActivatedOutlet = getCurrentOutlet( | |
rootContextMap, | |
target.constructor | |
); | |
if (!currentActivatedOutlet) | |
throw new Error('Component is not in router tree'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
cdr = compRef.injector.get(ChangeDetectorRef); | |
compRef.onDestroy(() => { | |
inited = false; | |
destroyer.next(); | |
}); | |
return routeWithData.data.pipe( | |
pluck(name || key), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
.subscribe({ | |
next: data => { | |
target[key] = data; | |
cdr.markForCheck(); | |
} | |
}); | |
... |
OlderNewer