File

src/lib/validation-error.directive.ts

Description

Directive allowing to define the template for an error of a given type (using the valError input), using an ng-template. It's used inside the body of the validation errors component, or inside the body of the default validation errors directive. See the documentation of these two for example usages.

Metadata

Index

Properties
Methods

Methods

Static ngTemplateContextGuard
ngTemplateContextGuard(_directive: ValidationErrorDirective, context)
Parameters :
Name Type Optional
_directive ValidationErrorDirective No
context No

Properties

Readonly templateRef
Default value : inject<TemplateRef<ValidationErrorContext>>(TemplateRef)
Readonly type
Default value : input.required<string>({ alias: 'valError' })

The type of the error that the content of the template must display.

import { Directive, input, TemplateRef, inject } from '@angular/core';

/**
 * The context of the ValidationErrorDirective
 */
interface ValidationErrorContext {
  /**
   * The label
   */
  $implicit: string | null;

  /**
   * The error
   */
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  error: any;
}

/**
 * Directive allowing to define the template for an error of a given type (using the `valError` input), using an ng-template.
 * It's used inside the body of the validation errors component, or inside the body of the default validation errors directive.
 * See the documentation of these two for example usages.
 */
@Directive({
  selector: 'ng-template[valError]'
})
export class ValidationErrorDirective {
  readonly templateRef = inject<TemplateRef<ValidationErrorContext>>(TemplateRef);

  /**
   * The type of the error that the content of the template must display.
   */
  readonly type = input.required<string>({ alias: 'valError' });

  static ngTemplateContextGuard(_directive: ValidationErrorDirective, context: unknown): context is ValidationErrorContext {
    return true;
  }
}

results matching ""

    No results matching ""