Skip to content

Commit 8ebd9df

Browse files
committed
feat(core): added *init directive
An example: <ng-container *init="let myvar of 'lol'">{{ myvar }}</ng-container>
1 parent 8c093b4 commit 8ebd9df

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1-
import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';
1+
import { Directive, Input, TemplateRef, ViewContainerRef, EmbeddedViewRef } from '@angular/core';
22

3-
@Directive({selector: '[init]'})
3+
interface InitContext {
4+
$implicit: any;
5+
}
6+
7+
@Directive({ selector: '[init]' })
48
export class InitDirective {
59

6-
@Input() set init(val: any) {
7-
this.viewContainer.clear();
8-
this.viewContainer.createEmbeddedView(this.templateRef, {init: val});
10+
@Input() set initOf(value: any) {
11+
this.context.$implicit = value;
12+
this.viewRef.markForCheck();
913
}
1014

15+
private context: InitContext = { $implicit: null };
16+
private viewRef: EmbeddedViewRef<InitContext> =
17+
this.viewContainerRef.createEmbeddedView(this.templateRef, this.context);
18+
1119
constructor(
12-
private templateRef: TemplateRef<any>,
13-
private viewContainer: ViewContainerRef
14-
) {
15-
}
20+
private templateRef: TemplateRef<InitContext>,
21+
private viewContainerRef: ViewContainerRef
22+
) { }
1623
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<ng-container *init="let myvar of 'LOL'">
2+
{{ myvar }}
3+
</ng-container>

0 commit comments

Comments
 (0)