11.3 C
Canberra
Thursday, June 18, 2026

Angular Indicators in apply: Constructing a signal-first type in Angular



// registration.mannequin.ts
export interface RegistrationData {
  e mail: string;
  password: string;
  confirmPassword: string;
  acceptedTerms: boolean;
}

This interface mirrors what would usually be despatched to a back-end API. There isn’t a duplication of state, no separate “type worth” object, and no mapping required at submission time.

Creating the signal-backed type

The shape itself is created within the element utilizing a writable sign because the supply of reality. The type() operate attaches type semantics validation, area state, and submission to that sign.

// registration.element.ts
import { CommonModule } from "@angular/widespread";
import { Part, sign } from "@angular/core";
import {
  e mail,
  type,
  FormField,
  required,
  submit,
} from "@angular/varieties/alerts";
import { RegistrationData } from "./registration.mannequin";

@Part({
  selector: "app-registration",
  imports: [FormField, CommonModule],
  templateUrl: "./registration.html",
  styleUrl: "./registration.css",
})
export class Registration {
  readonly mannequin = sign({
    e mail: "",
    password: "",
    confirmPassword: "",
    acceptedTerms: false,
  });

  readonly registrationForm = type(this.mannequin, (schema) => {
    required(schema.e mail, { message: "Electronic mail is required" });
    e mail(schema.e mail, { message: "Enter a legitimate e mail tackle" });

    required(schema.password, { message: "Password is required" });
    required(schema.confirmPassword, {
      message: "Please affirm your password",
    });

    required(schema.acceptedTerms, {
      message: "It's essential to settle for the phrases to proceed",
    });
  });

  async onSubmit(occasion?: Occasion) {
    occasion?.preventDefault();

    await submit(this.registrationForm, (worth) => {
      console.log(worth());
      // Mock Server Name
      return Promise.resolve([
        {
          kind: "EmailAlreadyExists",
          field: this.registrationForm.email,
          error: { kind: "server", message: "Email already taken" },
        },
      ]);
    });
  }
}

A number of design selections are price noting.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles