I want to know what purpose the context
parameter serves in createEmbeddedView()
method in Angular. The online angular docs do not provide this information.
For example I was reading this code where the author has made an iterator structural directive.
import {
Directive, ViewContainerRef, TemplateRef, Input, SimpleChange
} from "@angular/core";
@Directive({
selector: "[paForOf]"
})
export class PaIteratorDirective {
constructor(private container: ViewContainerRef, private template: TemplateRef<Object>) {
}
@Input("paForOf") dataSource: any;
ngOnInit() {
this.container.clear();
for (let i = 0; i < this.dataSource.length; i++) {
this.container.createEmbeddedView(this.template, new PaIteratorContext(this.dataSource[i]));
}
}
}
class PaIteratorContext {
constructor(public $implicit: any) { }
}
This is shown on the checkbox checked event on the template like this:
<div class="checkbox">
<label><input type="checkbox" [(ngModel)]="showTable" />Show Table</label>
</div>
<table *paIf="showTable" class="table table-sm table-bordered table-striped">
<tr>
<th></th>
<th>Name</th>
<th>Category</th>
<th>Price</th>
</tr>
<template [paForOf]="getProducts()" let-item>
<tr>
<td colspan="4">{{item.name}}</td>
</tr>
</template>
</table>
I want to understand this code:
this.container.createEmbeddedView(this.template, new PaIteratorContext(this.dataSource[i]));
Why i need to create an object of PaIteratorContext() class? why i can't just do this:
this.container.createEmbeddedView(this.template, this.dataSource[i]);
Please help ?