Skip to content

Instantly share code, notes, and snippets.

View alexzuza's full-sized avatar
🎯
Focusing

Alexey Zuev alexzuza

🎯
Focusing
View GitHub Profile
@alexzuza
alexzuza / di_platform_provider_example.ts
Created March 19, 2018 19:38
DI example of platform provider
const platform = platformBrowserDynamic([ {
provide: SharedService,
deps:[]
}]);
platform.bootstrapModule(AppModule);
platform.bootstrapModule(AppModule2);
@alexzuza
alexzuza / di_root_data.ts
Created March 19, 2018 19:41
DI root data
function createRootData(
elInjector: Injector, ngModule: NgModuleRef<any>, rendererFactory: RendererFactory2,
projectableNodes: any[][], rootSelectorOrNode: any): RootData {
const sanitizer = ngModule.injector.get(Sanitizer);
const errorHandler = ngModule.injector.get(ErrorHandler);
const renderer = rendererFactory.createRenderer(null, null);
return {
ngModule,
injector: elInjector, projectableNodes,
selectorOrNode: rootSelectorOrNode, sanitizer, rendererFactory, renderer, errorHandler
@alexzuza
alexzuza / di_simple_application.ts
Created March 19, 2018 19:42
DI simple application
@Component({
selector: 'my-app',
template: `<child></child>`,
})
export class AppComponent {}
@Component({
selector: 'child',
template: `<grand-child></grand-child>`
})
@alexzuza
alexzuza / di_bootstrap.ts
Created March 19, 2018 19:44
DI bootstrap
platformBrowserDynamic().bootstrapModule(AppModule);
@alexzuza
alexzuza / di_entry_component.ts
Created March 19, 2018 19:45
DI creating entry component
const compRef = componentFactory.create(Injector.NULL, [], selectorOrNode, ngModule);
@alexzuza
alexzuza / di_merge_injector.ts
Created March 19, 2018 19:46
DI merge injector
@Directive({
selector: '[someDir]'
}
export class SomeDirective {
constructor(private injector: Injector) {}
}
@alexzuza
alexzuza / di_injector_element.ts
Created March 19, 2018 20:14
DI injector element definition
export interface ElementDef {
...
/**
* visible public providers for DI in the view,
* as see from this element. This does not include private providers.
*/
publicProviders: {[tokenKey: string]: NodeDef}|null;
/**
* same as visiblePublicProviders, but also includes private providers
* that are located on this element.
@alexzuza
alexzuza / di_element_injector_resolver.ts
Created March 19, 2018 20:16
DI element injector resolver
const providerDef =
(allowPrivateServices ? elDef.element!.allProviders :
elDef.element!.publicProviders)![tokenKey];
if (providerDef) {
let providerData = asProviderData(searchView, providerDef.nodeIndex);
if (!providerData) {
providerData = { instance: _createProviderInstance(searchView, providerDef) };
searchView.nodes[providerDef.nodeIndex] = providerData as any;
}
return providerData.instance;
@alexzuza
alexzuza / di_view_parent_element.ts
Last active March 19, 2018 21:04
DI view parent element
/**
* for component views, this is the host element.
* for embedded views, this is the index of the parent node
* that contains the view container.
*/
export function viewParentEl(view: ViewData): NodeDef|null {
const parentView = view.parent;
if (parentView) {
return view.parentNodeDef !.parent;
} else {
@alexzuza
alexzuza / di_view_parent_element_hierarhy.ts
Last active March 20, 2018 05:15
DI view parent element hierarhy
@Component({
selector: 'my-app',
template: `<my-list></my-list>`
})
export class AppComponent {}
@Component({
selector: 'my-list',
template: `
<div class="container">