当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript Injector.create方法代码示例

本文整理汇总了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;
}
开发者ID:BobChao87,项目名称:angular,代码行数:33,代码来源:car-injector.ts

示例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)
}
开发者ID:zodiac-team,项目名称:zodiac-ui,代码行数:29,代码来源:renderers.ts

示例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
        }
    }
开发者ID:zodiac-team,项目名称:zodiac-ui,代码行数:58,代码来源:code-panel.component.ts

示例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)
                    })
                }
            })
        }
    }
开发者ID:zodiac-team,项目名称:zodiac-ui,代码行数:57,代码来源:renderers.ts

示例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
 });
开发者ID:hulkike,项目名称:angular,代码行数:9,代码来源:injector_spec.ts

示例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);
    });
开发者ID:ijavascripter,项目名称:ng-dynamic-component,代码行数:11,代码来源:dynamic.component.spec.ts

示例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)
    }
开发者ID:zodiac-team,项目名称:zodiac-ui,代码行数:13,代码来源:editor-node-view.component.ts

示例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'}));
 });
开发者ID:Cammisuli,项目名称:angular,代码行数:14,代码来源:mock_backend_spec.ts


注:本文中的@angular/core.Injector.create方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。