src/app/directives/ng-var.directive.ts

Description

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

Example

Metadata

Relationships

Depends on

No results matching.

Index

Properties
Inputs
Accessors

Constructor

constructor(templateRef: TemplateRef, vcRef: ViewContainerRef)
Parameters :
Name Type Optional
templateRef TemplateRef<any> No
vcRef ViewContainerRef No

Inputs

ngVar
Type : unknown

Properties

Private context
Type : { $implicit: unknown; ngVar: unknown }
Default value : { $implicit: null, ngVar: null }
Private hasView
Type : boolean
Default value : false

Accessors

ngVar
setngVar(context: unknown)
Parameters :
Name Type Optional
context unknown No
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
  ) {}
}

results matching ""

    No results matching ""