Skip to content

Commit 38e562d

Browse files
harunurhanvalorkin
authored andcommitted
feat(popover): support passing template context (#2428)
* Fixes #1682 by adding a new @input for popover directive so that context can be passed when content is TemplateRef.
1 parent f398576 commit 38e562d

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/component-loader/component-loader.class.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ export class ComponentLoader<T> {
104104
}
105105

106106
// todo: appendChild to element or document.querySelector(this.container)
107-
public show(opts: { content?: string | TemplateRef<any>, [key: string]: any } = {}): ComponentRef<T> {
107+
public show(opts: { content?: string | TemplateRef<any>, context?: any, [key: string]: any } = {}): ComponentRef<T> {
108108
this._subscribePositioning();
109109
this._innerComponent = null;
110110

111111
if (!this._componentRef) {
112112
this.onBeforeShow.emit();
113-
this._contentRef = this._getContentRef(opts.content);
113+
this._contentRef = this._getContentRef(opts.content, opts.context);
114114
const injector = ReflectiveInjector.resolveAndCreate(this._providers, this._injector);
115115

116116
this._componentRef = this._componentFactory.create(injector, this._contentRef.nodes);
@@ -289,14 +289,15 @@ export class ComponentLoader<T> {
289289
this._zoneSubscription = null;
290290
}
291291

292-
private _getContentRef(content: string | TemplateRef<any> | any): ContentRef {
292+
private _getContentRef(content: string | TemplateRef<any> | any, context?: any): ContentRef {
293293
if (!content) {
294294
return new ContentRef([]);
295295
}
296296

297297
if (content instanceof TemplateRef) {
298298
if (this._viewContainerRef) {
299-
const viewRef = this._viewContainerRef.createEmbeddedView<TemplateRef<T>>(content);
299+
const viewRef = this._viewContainerRef.createEmbeddedView<TemplateRef<T>>(content, context);
300+
viewRef.markForCheck();
300301
return new ContentRef([viewRef.rootNodes], viewRef);
301302
}
302303
const viewRef = content.createEmbeddedView({});

src/popover/popover.directive.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export class PopoverDirective implements OnInit, OnDestroy {
1515
* Content to be displayed as popover.
1616
*/
1717
@Input() public popover: string | TemplateRef<any>;
18+
/**
19+
* Context to be used if popover is a template.
20+
*/
21+
@Input() public popoverContext: any;
1822
/**
1923
* Title of a popover.
2024
*/
@@ -103,6 +107,7 @@ export class PopoverDirective implements OnInit, OnDestroy {
103107
.position({attachment: this.placement})
104108
.show({
105109
content: this.popover,
110+
context: this.popoverContext,
106111
placement: this.placement,
107112
title: this.popoverTitle,
108113
containerClass: this.containerClass

0 commit comments

Comments
 (0)