當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。