Skip to content

Jaspero/ng2-form-builder

Repository files navigation

Build Status NPM Version

NG2 Form-Builder

This library provides a faster and cleaner way to use Angular 2 Reactive Forms through typescript decorators.

Setup

Import FormBuilderModule in your @NgModule and you're good to go:

@NgModule({
    imports: [
        FormBuilderModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule {}

How to use

To use this library simply create a class and add @JasperoBuilder.dec() decorators to each property:

export class User {
    @JasperoBuilder.dec({initial: ''})
    username: string;
    @JasperoBuilder.dec({initial: ''})
    password: string;
}

The dec() method accepts the same arguments as new FormControl() does and creates the FormControl in the background.

Then in your component create the FormGroup like this:

export class AppComponent {
    constructor(private _jb: JasperoBuilder) {}
    
    userForm: any;

    ngOnInit() {
        // You can also instantiate the class with values
        this.useForm = this._jb.createForm(new User());
    }
}

Releases

No releases published

Sponsor this project

 

Packages

No packages published