0.0.1 • Published 9 years ago
forms-decor v0.0.1
forms-decor
Model decorators for forms auto-generation for Angular 2
Installation
npm i --save forms-decorUsage
- Define your model class:
import { FDControl } from 'forms-decor';
import { Validators } from '@angular/forms';
export class User {
id: number;
@FDControl({validators: Validators.required}) name: string;
@FDControl({validators: Validators.required}) password: string;
}- Generate Angular form:
import { Component } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { fromModel } from 'forms-decor';
import { User } from './user';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
form: FormGroup;
constructor() {
this.form = new FormGroup(fromModel(new User()));
}
}- This will be equivalent to:
// ...
constructor() {
this.form = new FormGroup({
name: new FormControl('', Validators.required),
password: new FormControl('', Validators.required)
});
}
// ...Decorator configuration
@FDControl also supports additional configuration options:
name- overrides model property name with a specified string value. It will be passed toFormGroupcontructor instead of property name.overrideValue- overrides property value passed toFormControlconstructor. Warning! This will override value for ALL instances of your model!
0.0.1
9 years ago