Giter VIP home page Giter VIP logo

ngx-strong-forms's Introduction

ngx-strong-forms

Getting Started

Installation Instructions

Install ngx-strong-forms from npm:

npm install ngx-strong-forms --save

Usage

This library provides 4 different types of form control.

TypedFormArray

This uses the Angular FormArray under the hood. It represents an array of items with type T, items can be added and removed but they must all be of type T.

TypedFormControl

This uses the Angular FormControl under the hood. It represents a single item of type T.

TypedFormDictionary

This uses the Angular FormGroup under the hood. It represents a dictionary of items of type T with a string key, items can be added and removed but they must all be of type T.

TypedFormGroup

This uses the Angular FormGroup under the hood. It represents a group of other form controls, once it is created items cannot be added or removed.

Examples

import { TypedFormControl, TypedFormGroup, TypedFormDictionary } from 'ngx-strong-forms';

const form = new TypedFormGroup({
  name: new TypedFormControl<string>(),
  details: new TypedFormGroup({
    size: new TypedFormControl<'small' | 'medium' | 'large'>(),
    weight: new TypedFormControl<number>()
  }),
  locations: new TypedFormDictionary({
    usa: new TypedFormGroup({
      count: new TypedFormControl<number>()
    }),
    japan: new TypedFormGroup({
      count: new TypedFormControl<number>()
    })
  })
});

form.controls.name.value // type is string

form.controls.details.controls.weight.valueChanges // type is Observable<number>

form.value // type is:
// {
//   name: string,
//   details: {
//     size: 'small' | 'medium' | 'large',
//     weight: number
//   },
//   locations: {
//     [key: string]: {
//       count: number
//     }
//   }
// }

form.controls.locations.addControl('brazil', new TypedFormControl(0)); // error: argument must be of type TypedFormGroup<{ count: TypedFormControl<number> }>

To access the underlying Angular control, just use the ng property:

<div [formGroup]="form.ng">
  <input type="text" formControlName="name">
  <ul>
    <li *ngFor="let location of form.get('locations').controls | keyvalue">
      <span>Name: {{location.key}}</span>
      <input type="number" [formControl]="location.value.controls.count.ng">
    </li>
  </ul>
</div>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.