Skip to content

Commit

Permalink
feat(directive): Added component creation event
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswhite199 committed May 2, 2018
1 parent 077060e commit 52a1951
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,25 @@ class MyDynamicComponent1 {
Here you can update your inputs (ex. `inputs.hello = 'WORLD'`) and they will trigger standard Angular's life-cycle hooks
(of course you should consider which change detection strategy you are using).

### Component Creation Events

You can subscribe to component creation events, being passed a reference to the `ComponentRef`:

```ts
@Component({
selector: 'my-component',
template: `<ndc-dynamic [ndcDynamicComponent]="component"
[ndcDynamicCreated]="componentCreated($event)"
></ndc-dynamic>`
})
class MyComponent {
component = MyDynamicComponent1;
componentCreated(compRef: ComponentRef<any>) {
// utilize compRef in some way ...
}
}
```

### Attributes

**Since v2.2.0** you can now declaratively set attributes, as you would inputs, via `ndcDynamicAttributes`:
Expand Down
15 changes: 15 additions & 0 deletions src/dynamic/dynamic.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
Component,
ComponentRef,
InjectionToken,
Injector,
NO_ERRORS_SCHEMA,
Expand All @@ -26,6 +27,7 @@ describe('DynamicComponent', () => {
let testTemplate = `<ndc-dynamic [ndcDynamicComponent]="component"
[ndcDynamicInjector]="injector"
[ndcDynamicProviders]="providers"
(ndcDynamicCreated)="createdComponent($event)"
[ndcDynamicContent]="content"></ndc-dynamic>`;
let fixture: ComponentFixture<TestComponent>, createComp = true;

Expand Down Expand Up @@ -60,6 +62,13 @@ describe('DynamicComponent', () => {
expect(injectedElem.componentInstance).toEqual(jasmine.any(InjectedComponent));
});

it('should emit event when component created', () => {
fixture.componentInstance.component = InjectedComponent;
fixture.detectChanges();

expect(fixture.componentInstance.comp.instance).toBeInstanceOf(InjectedComponent);
});

it('should clear view if [ndcDynamicComponent] becomes null', () => {
fixture.componentInstance.component = InjectedComponent;
fixture.detectChanges();
Expand Down Expand Up @@ -181,7 +190,13 @@ class TestComponent {
providers: Provider[];
content: any[][];

comp: ComponentRef<any>;

@ViewChildren(TemplateRef) tplRefs: QueryList<TemplateRef<any>>;

constructor(public vcRef: ViewContainerRef) { }

createdComponent(comp: ComponentRef<any>) {
this.comp = comp;
}
}
5 changes: 5 additions & 0 deletions src/dynamic/dynamic.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import {
Component,
ComponentFactoryResolver,
ComponentRef,
EventEmitter,
Injector,
Input,
OnChanges,
Output,
Provider,
ReflectiveInjector,
SimpleChanges,
Expand All @@ -24,6 +26,8 @@ export class DynamicComponent implements OnChanges, ComponentInjector {
@Input() ndcDynamicProviders: Provider[];
@Input() ndcDynamicContent: any[][];

@Output() ndcDynamicCreated: EventEmitter<ComponentRef<any>> = new EventEmitter();

componentRef: ComponentRef<any> | null;

constructor(
Expand All @@ -46,6 +50,7 @@ export class DynamicComponent implements OnChanges, ComponentInjector {
this._cfr.resolveComponentFactory(this.ndcDynamicComponent),
0, this._resolveInjector(), this.ndcDynamicContent
);
this.ndcDynamicCreated.emit(this.componentRef);
}
}

Expand Down

0 comments on commit 52a1951

Please sign in to comment.