src/app/directives/ng-var.directive.ts
Purpose of this directive is ability to declare variables in html. It helps writing cleaner code and prevents us from calling function is html Calling functions in html is bad for performance
| Selector | [ngVar] |
No results matching.
Properties |
Inputs |
Accessors |
constructor(templateRef: TemplateRef
|
|||||||||
|
Defined in src/app/directives/ng-var.directive.ts:31
|
|||||||||
|
Parameters :
|
| ngVar | |
Type : unknown
|
|
|
Defined in src/app/directives/ng-var.directive.ts:14
|
|
| Private context |
Type : { $implicit: unknown; ngVar: unknown }
|
Default value : {
$implicit: null,
ngVar: null
}
|
|
Defined in src/app/directives/ng-var.directive.ts:23
|
| Private hasView |
Type : boolean
|
Default value : false
|
|
Defined in src/app/directives/ng-var.directive.ts:31
|
| ngVar | ||||||
setngVar(context: unknown)
|
||||||
|
Defined in src/app/directives/ng-var.directive.ts:14
|
||||||
|
Parameters :
Returns :
void
|
import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core'
/**
* Purpose of this directive is ability to declare variables in html.
* It helps writing cleaner code and prevents us from calling function is html
* Calling functions in html is bad for performance
*/
@Directive({
selector: '[ngVar]',
standalone: false
})
export class NgVarDirective {
@Input()
set ngVar(context: unknown) {
this.context.$implicit = this.context.ngVar = context
if (!this.hasView) {
this.vcRef.createEmbeddedView(this.templateRef, this.context)
this.hasView = true
}
}
private context: {
$implicit: unknown
ngVar: unknown
} = {
$implicit: null,
ngVar: null
}
private hasView: boolean = false
constructor(
private templateRef: TemplateRef<any>,
private vcRef: ViewContainerRef
) {}
}