Skip to content

Commit 773a4cc

Browse files
committed
feat(core): added new syntax for init: *init="EXPRESSION as variable"
1 parent 87f3595 commit 773a4cc

2 files changed

Lines changed: 46 additions & 17 deletions

File tree

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

3-
interface InitContext {
4-
$implicit: any;
3+
interface InitContext<T> {
4+
$implicit: T;
5+
init: T;
56
}
67

78
@Directive({ selector: '[init]' })
8-
export class InitDirective implements OnDestroy {
9+
export class InitDirective<T> implements OnDestroy {
910

10-
@Input() set initOf(value: any) {
11-
this.context.$implicit = value;
12-
this.viewRef.markForCheck();
11+
@Input() set init(value: T) {
12+
this.setValue(value);
1313
}
1414

15-
private context: InitContext = { $implicit: null };
16-
private viewRef: EmbeddedViewRef<InitContext> =
15+
@Input() set initOf(value: T) {
16+
this.setValue(value);
17+
}
18+
19+
private context: InitContext<T> = { $implicit: null, init: null };
20+
private viewRef: EmbeddedViewRef<InitContext<T>> =
1721
this.viewContainer.createEmbeddedView(this.templateRef, this.context);
1822

1923
constructor(
20-
private templateRef: TemplateRef<InitContext>,
24+
private templateRef: TemplateRef<InitContext<T>>,
2125
private viewContainer: ViewContainerRef
2226
) {}
2327

@@ -29,4 +33,11 @@ export class InitDirective implements OnDestroy {
2933
}
3034
}
3135

36+
private setValue(value: T): void {
37+
this.context.$implicit = this.context.init = value;
38+
if (this.viewRef) {
39+
this.viewRef.markForCheck();
40+
}
41+
}
42+
3243
}

projects/platform/src/test/directives/init.directive.spec.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,38 @@ describe('InitDirective', () => {
1515
host: Host
1616
});
1717

18-
it('should create variable through template', () => {
19-
host = create(`<ng-container *init="let variable of '${VALUE_IN_TEMPLATE}'">{{ variable }}</ng-container>`);
18+
describe('using letOf', () => {
19+
it('should create variable through template', () => {
20+
host = create(`<ng-container *init="let variable of '${VALUE_IN_TEMPLATE}'">{{ variable }}</ng-container>`);
2021

21-
expect(host.hostElement).toHaveText(VALUE_IN_TEMPLATE);
22+
expect(host.hostElement).toHaveText(VALUE_IN_TEMPLATE);
23+
});
24+
25+
it('should create variable through binding', () => {
26+
host = create(`<ng-container *init="let variable of example">{{ variable }}</ng-container>`);
27+
28+
expect(host.hostElement).not.toHaveText(VALUE_THROUGH_BINDING);
29+
30+
host.setHostInput({ example: VALUE_THROUGH_BINDING });
31+
expect(host.hostElement).toHaveText(VALUE_THROUGH_BINDING);
32+
});
2233
});
2334

24-
it('should create variable through binding', () => {
25-
host = create(`<ng-container *init="let variable of example">{{ variable }}</ng-container>`);
35+
describe('using as', () => {
36+
it('should create variable through template', () => {
37+
host = create(`<ng-container *init="'${VALUE_IN_TEMPLATE}' as variable">{{ variable }}</ng-container>`);
38+
39+
expect(host.hostElement).toHaveText(VALUE_IN_TEMPLATE);
40+
});
2641

27-
expect(host.hostElement).not.toHaveText(VALUE_THROUGH_BINDING);
42+
it('should create variable through binding', () => {
43+
host = create(`<ng-container *init="example as variable">{{ variable }}</ng-container>`);
2844

29-
host.setHostInput({ example: VALUE_THROUGH_BINDING });
45+
expect(host.hostElement).not.toHaveText(VALUE_THROUGH_BINDING);
3046

31-
expect(host.hostElement).toHaveText(VALUE_THROUGH_BINDING);
47+
host.setHostInput({ example: VALUE_THROUGH_BINDING });
48+
expect(host.hostElement).toHaveText(VALUE_THROUGH_BINDING);
49+
});
3250
});
3351

3452
});

0 commit comments

Comments
 (0)