@ngx-formly is an Angular module which has a Components to help customize and render JavaScript/JSON configured forms.
The formly-form
Component and the FormlyConfig
service are very powerful and bring unmatched maintainability to your
application's forms.
Follow these steps to get started with ng-formly. Also check out our guides and demos for further examples.
npm install @angular/forms @ngx-formly/core --save
- Material2:
- Ensure you have already installed material2 https://material.angular.io/guide/getting-started
- Install
@ngx-formly/material
npm install @ngx-formly/material --save
npm install @ngx-formly/bootstrap --save
import {NgModule} from '@angular/core';
import {ReactiveFormsModule} from '@angular/forms';
import {FormlyModule} from '@ngx-formly/core';
import {FormlyBootstrapModule} from '@ngx-formly/bootstrap';
// for material2 import `FormlyMaterialModule`:
// import {FormlyMaterialModule} from '@ngx-formly/material';
@NgModule({
imports: [
...,
ReactiveFormsModule,
FormlyModule.forRoot(),
FormlyBootstrapModule,
// for material2 use:
// FormlyMaterialModule
],
})
export class AppModule {}
import {Component} from '@angular/core';
import {FormGroup} from '@angular/forms';
import {FormlyFieldConfig} from '@ngx-formly/core';
@Component({
selector: 'app',
template: `
<form [formGroup]="form" (ngSubmit)="submit(userModel)">
<formly-form [model]="userModel" [fields]="userFields">
<button type="submit" class="btn btn-default">Submit</button>
</formly-form>
</form>
`,
})
export class AppComponent {
form = new FormGroup({});
userModel = { email: '[email protected]' };
userFields: Array<FormlyFieldConfig> = [{
key: 'email',
type: 'input',
templateOptions: {
type: 'email',
label: 'Email address',
placeholder: 'Enter email',
required: true,
}
}];
submit(user) {
console.log(user);
}
}
From there, it's just JavaScript. Allowing for DRY, maintainable, reusable forms.
See the issues labeled enhancement
A special thanks to Kent C. Dodds for giving me opportunity to work on this. This library is maintained (with love) by me, Mohammed Zama Khan. Thanks to all contributors! If you're trying to find angular-formly, go here
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!