Primitives

Form Field

Please include any middle names, no matter how ridiculous.

This field is required.

Import

Import the FormField primitives from ng-primitives/form-field.

import {
  NgpFormField,
  NgpLabel,
  NgpDescription,
  NgpError,
  NgpFormControl,
} from 'ng-primitives/form-field';

Usage

Assemble the form-field directives in your template.

<div ngpFormField>
  <label ngpLabel>Label</label>
  <!-- Typically ngpFormControl would not be used directly, but a primitive like ngpInput would be used instead -->
  <input ngpFormControl />
  <p ngpDescription>Description</p>
  <p ngpError ngpErrorValidator="required">Error</p>
</div>

Reusable Component

Create a reusable component that uses the NgpFormField directive.

import { Component } from '@angular/core';
import { FormField } from './form-field';

@Component({
  selector: 'app-root',
  imports: [FormField],
  template: `
    <app-form-field>
      <!-- Add label and form control here -->
      <label>Username</label>
      <input type="text" placeholder="Enter your username" />
    </app-form-field>
  `,
})
export default class App {}

Schematics

Generate a reusable form-field component using the Angular CLI.

ng g ng-primitives:primitive form-field

Options

  • path: The path at which to create the component file.
  • prefix: The prefix to apply to the generated component selector.
  • componentSuffix: The suffix to apply to the generated component class name.
  • fileSuffix: The suffix to apply to the generated component file name. Defaults to component.
  • exampleStyles: Whether to include example styles in the generated component file. Defaults to true.

API Reference

The following directives are available to import from the ng-primitives/form-field package:

NgpFormField

The `NgpFormField` directive is a container for form field elements. Any labels, form controls, or descriptions should be placed within this directive.

  • Selector: [ngpFormField]
  • Exported As: ngpFormField

Data Attributes

Attribute Description
data-invalid Applied when the form control is invalid.
data-valid Applied when the form control is valid.
data-touched Applied when the form control is touched.
data-pristine Applied when the form control is pristine.
data-dirty Applied when the form control is dirty.
data-pending Applied when the form control is pending.
data-disabled Applied when the form control is disabled.

NgpLabel

The `NgpLabel` directive is used to mark a label element within a form field. Preferably, there should use an HTML `<label>` element.

  • Selector: [ngpLabel]
  • Exported As: ngpLabel

Data Attributes

Attribute Description Value
data-invalid Applied when the form control is invalid.
data-valid Applied when the form control is valid.
data-touched Applied when the form control is touched.
data-pristine Applied when the form control is pristine.
data-dirty Applied when the form control is dirty.
data-pending Applied when the form control is pending.
data-disabled Applied when the form control is disabled.

NgpDescription

The `NgpDescription` directive is used to mark a description element within a form field. There may be multiple descriptions associated with a form control.

  • Selector: [ngpDescription]
  • Exported As: ngpDescription

Data Attributes

Attribute Description Value
data-invalid Applied when the form control is invalid.
data-valid Applied when the form control is valid.
data-touched Applied when the form control is touched.
data-pristine Applied when the form control is pristine.
data-dirty Applied when the form control is dirty.
data-pending Applied when the form control is pending.
data-disabled Applied when the form control is disabled.

NgpError

The `NgpError` directive is used to mark an error message element within a form field. There may be multiple error messages associated with a form control.

  • Selector: [ngpError]
  • Exported As: ngpError
ngpErrorValidator
string | null

The validator associated with the error message.

Data Attributes

Attribute Description Value
data-validator Whether the validator specified in ngpErrorValidator is failing. fail | pass
data-invalid Applied when the form control is invalid. -
data-valid Applied when the form control is valid. -
data-touched Applied when the form control is touched. -
data-pristine Applied when the form control is pristine. -
data-dirty Applied when the form control is dirty. -
data-pending Applied when the form control is pending. -
data-disabled Applied when the form control is disabled. -

NgpFormControl

Typically this primitive would be not be used directly, but instead a more specific form control primitive would be used (e.g. `ngpInput`). All of our form control primitives use `ngpFormControl` internally so they will have the same accessibility features as described below. The `NgpFormControl` directive is used to mark a form control element within a form field. This element will have an `aria-labelledby` attribute set to the ID of the label element within the form field and an `aria-describedby` attribute set to the ID of the description elements within the form field.

  • Selector: [ngpFormControl]
  • Exported As: ngpFormControl
ngpFormControlDisabled
boolean

Whether the form control is disabled by a parent.

Data Attributes

Attribute Description Value
data-invalid Applied when the form control is invalid.
data-valid Applied when the form control is valid.
data-touched Applied when the form control is touched.
data-pristine Applied when the form control is pristine.
data-dirty Applied when the form control is dirty.
data-pending Applied when the form control is pending.
data-disabled Applied when the form control is disabled.

Accessibility

The label and description elements should be associated with the form control using the aria-labelledby and aria-describedby attributes, respectively. This will ensure that screen readers can provide the necessary context to users.

Copyright © 2025 Angular Primitives