本文整理匯總了TypeScript中angular2/di.Injector類的典型用法代碼示例。如果您正苦於以下問題:TypeScript Injector類的具體用法?TypeScript Injector怎麽用?TypeScript Injector使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Injector類的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: it
it('should bind to an alias', function() {
var injector = new Injector([Engine, bind(SportsCar).toClass(SportsCar), bind(Car).toAlias(SportsCar)]);
var car = injector.get(Car);
var sportsCar = injector.get(SportsCar);
expect(car).toBeAnInstanceOf(SportsCar);
expect(car).toBe(sportsCar);
});
示例2: createInjector
export function createInjector(zone: NgZone, bus: MessageBus): Injector {
BrowserDomAdapter.makeCurrent();
_rootBindings.push(bind(NgZone).toValue(zone));
_rootBindings.push(bind(MessageBus).toValue(bus));
var injector: Injector = Injector.resolveAndCreate(_rootBindings);
return injector.resolveAndCreateChild(_injectorBindings());
}
示例3: _createAppInjector
function _createAppInjector(appComponentType, bindings, zone) {
if (isBlank(_rootInjector))
_rootInjector = new Injector(_rootBindings);
var mergedBindings = isPresent(bindings) ? ListWrapper.concat(_injectorBindings(appComponentType), bindings) : _injectorBindings(appComponentType);
ListWrapper.push(mergedBindings, bind(VmTurnZone).toValue(zone));
return _rootInjector.createChild(mergedBindings);
}
示例4: it
it('should return a promise when instantiating a sync binding ' + 'with an async dependency', inject([AsyncTestCompleter], (async) => {
var injector = new Injector([bind(UserList).toAsyncFactory(fetchUsers), UserController]);
injector.asyncGet(UserController).then(function(userController) {
expect(userController).toBeAnInstanceOf(UserController);
expect(userController.list).toBeAnInstanceOf(UserList);
async.done();
});
}));
示例5: BaseException
/**
* Creates an `AppView` for the given component.
*
* Only either a component or a context needs to be specified but both can be provided for
* advanced use cases (ie subclassing the context).
*
* @param {Type} component
* @param {*} context
* @param {string} html Use as the component template when specified (shortcut for
* setInlineTemplate)
* @return {Promise<ViewProxy>}
*/
createView(component: Type,
{context = null,
html = null}: {context?: any, html?: string} = {}): Promise<ViewProxy> {
if (isBlank(component) && isBlank(context)) {
throw new BaseException('You must specified at least a component or a context');
}
if (isBlank(component)) {
component = getTypeOf(context);
} else if (isBlank(context)) {
context = instantiateType(component);
}
if (isPresent(html)) {
this.setInlineTemplate(component, html);
}
var doc = this._injector.get(DOCUMENT_TOKEN);
var rootEl = el('<div id="root"></div>');
DOM.appendChild(doc.body, rootEl);
var componentBinding = bind(component).toValue(context);
return this._injector.get(DynamicComponentLoader)
.loadAsRoot(componentBinding, '#root', this._injector)
.then((hostComponentRef) => { return new ViewProxy(hostComponentRef); });
}