本文整理匯總了TypeScript中@angular/core.Injector.create方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Injector.create方法的具體用法?TypeScript Injector.create怎麽用?TypeScript Injector.create使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類@angular/core.Injector
的用法示例。
在下文中一共展示了Injector.create方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: useInjector
export function useInjector() {
let injector: Injector;
// #enddocregion injector
/*
// #docregion injector-no-new
// Cannot instantiate an Injector like this!
let injector = new Injector([
{ provide: Car, deps: [Engine, Tires] },
{ provide: Engine, deps: [] },
{ provide: Tires, deps: [] }
]);
// #enddocregion injector-no-new
*/
// #docregion injector, injector-create-and-call
injector = Injector.create({
providers: [
{ provide: Car, deps: [Engine, Tires] },
{ provide: Engine, deps: [] },
{ provide: Tires, deps: [] }
]
});
// #docregion injector-call
let car = injector.get(Car);
// #enddocregion injector-call, injector-create-and-call
car.description = 'Injector';
injector = Injector.create({
providers: [{ provide: Logger, deps: [] }]
});
let logger = injector.get(Logger);
logger.log('Injector car.drive() said: ' + car.drive());
return car;
}
示例2: createRenderer
export function createRenderer(formula: Formula, parentInjector: Injector): FormulaRenderer {
const DIFFER = provideDiffer(formula, parentInjector)
const RENDERER = provideRenderer(formula)
const injector = Injector.create({
parent: parentInjector,
providers: [
RENDERER,
DIFFER,
{
provide: FORMULA,
useValue: formula,
},
],
})
if (formula.type === FormulaType.ARRAY) {
return injector.get(FormulaArrayRenderer)
}
if (formula.type === FormulaType.GROUP) {
return injector.get(FormulaGroupRenderer)
}
if (formula.type === FormulaType.CONTAINER) {
return injector.get(FormulaContainerRenderer)
}
return injector.get(FormulaDefaultRenderer)
}
示例3: return
return (editor: EditorService) => {
const pluginState: CodeBlockState = pluginKey.getState(editor.state)
const origin = pluginState.element
if (!pluginState.toolbarVisible) {
overlayRef.detach()
return
}
const toolbar = new FloatingToolbar(editor, overlayRef)
const injector = Injector.create({
parent: parentInjector,
providers: [
{
provide: FloatingToolbar,
useValue: toolbar,
},
{
provide: EditorToolbar,
useValue: toolbar,
},
],
})
const linkPanelPortal = new ComponentPortal(CodePanelComponent, null, injector)
const positions = [
new ConnectionPositionPair(
{ originX: "center", originY: "bottom" },
{ overlayX: "center", overlayY: "top" },
),
]
if (previousOrigin !== origin) {
overlayRef.detach()
}
if (!overlayRef.hasAttached()) {
overlayRef.attach(linkPanelPortal)
const strategy = overlay
.position()
.flexibleConnectedTo(origin)
.withFlexibleDimensions(false)
.withPush(false)
.withPositions(positions)
const scrollStrategy = overlay.scrollStrategies.reposition()
strategy.attach(overlayRef)
strategy.apply()
scrollStrategy.attach(overlayRef)
scrollStrategy.enable()
overlayRef.updatePositionStrategy(strategy)
previousOrigin = origin
}
}
示例4: render
render(node: FormulaArrayNode) {
const parent = this.injector.get(FORMULA_NODE, null)
const injector = Injector.create({
parent: this.injector,
providers: [
{
provide: FormulaContext,
useValue: {
model: node.model,
data: node.formula.data,
resolve: {},
},
},
{
provide: FORMULA,
useValue: node.formula,
},
{
provide: FORMULA_NODE,
useValue: node,
},
],
})
if (node.formula.component && node !== parent) {
const factory = this.cfr.resolveComponentFactory(node.formula.component)
const component = this.vcr.createComponent(factory, null, injector)
if (node.formula.class) {
node.formula.class
.split(" ")
.forEach(className =>
this.renderer.addClass(component.location.nativeElement, className),
)
}
this.refs.set(node, component)
} else {
node.model.valueChanges.pipe(startWith(null)).subscribe(() => {
const diff = this.differ.diff(node.children)
if (diff) {
diff.forEachRemovedItem(record => {
this.removeChild(record.item)
})
diff.forEachMovedItem(record => {
this.moveChild(record.item, record.currentIndex)
})
diff.forEachAddedItem(record => {
this.addChild(record.item)
})
}
})
}
}
示例5: it
it('works', () => {
// #docregion Injector
const injector: Injector =
Injector.create({providers: [{provide: 'validToken', useValue: 'Value'}]});
expect(injector.get('validToken')).toEqual('Value');
expect(() => injector.get('invalidToken')).toThrowError();
expect(injector.get('invalidToken', 'notFound')).toEqual('notFound');
// #enddocregion
});
示例6: it
it('should use [ndcDynamicInjector] if provided', () => {
fixture.componentInstance.component = InjectedComponent;
fixture.componentInstance.injector = Injector.create([
{ provide: token, useValue: tokenValue }
], fixture.componentRef.injector);
fixture.detectChanges();
const injectedElem = fixture.debugElement.query(By.directive(InjectedComponent));
expect(injectedElem.injector.get(token)).toBe(tokenValue);
});
示例7: createComponent
public createComponent() {
const injector = Injector.create({
parent: this.parentInjector,
providers: [
{
provide: EditorNodeViewContext,
useValue: new EditorNodeViewContext(this.node, this.view, this.getPos),
},
],
})
this.componentRef = this.componentFactory.create(injector, null, this.node, this.ngModule)
}
示例8: beforeEach
beforeEach(() => {
const injector = Injector.create([
{provide: ResponseOptions, useClass: BaseResponseOptions, deps: []},
{provide: MockBackend, deps: []}
]);
backend = injector.get(MockBackend);
const base = new BaseRequestOptions();
sampleRequest1 =
new Request(base.merge(new RequestOptions({url: 'https://google.com'})) as any);
sampleResponse1 = new Response(new ResponseOptions({body: 'response1'}));
sampleRequest2 =
new Request(base.merge(new RequestOptions({url: 'https://google.com'})) as any);
sampleResponse2 = new Response(new ResponseOptions({body: 'response2'}));
});