35 lines
1.3 KiB
TypeScript
Raw Normal View History

import {Injector, Directive, ViewContainerRef, Attribute, ComponentRef, ComponentFactory, ResolvedReflectiveProvider, ReflectiveInjector} from '@angular/core';
2016-05-24 13:21:38 -07:00
import {RouterOutletMap} from '../router_outlet_map';
import {PRIMARY_OUTLET} from '../shared';
2016-05-24 13:21:38 -07:00
@Directive({selector: 'router-outlet'})
export class RouterOutlet {
private activated:ComponentRef<any>|null;
public outletMap:RouterOutletMap;
constructor(parentOutletMap:RouterOutletMap, private location:ViewContainerRef,
@Attribute('name') name:string, public injector: Injector) {
2016-05-24 13:21:38 -07:00
parentOutletMap.registerOutlet(name ? name : PRIMARY_OUTLET, this);
}
get isActivated(): boolean { return !!this.activated; }
2016-06-02 14:44:57 -07:00
get component(): Object {
if (!this.activated) throw new Error("Outlet is not activated");
return this.activated.instance;
}
2016-05-24 13:21:38 -07:00
deactivate(): void {
if (this.activated) {
this.activated.destroy();
this.activated = null;
}
}
activate(factory: ComponentFactory<any>, providers: ResolvedReflectiveProvider[],
outletMap: RouterOutletMap): void {
this.outletMap = outletMap;
let inj = ReflectiveInjector.fromResolvedProviders(providers, this.location.parentInjector);
this.activated = this.location.createComponent(factory, this.location.length, inj, []);
}
}