-
Notifications
You must be signed in to change notification settings - Fork 2
Home
ngx-form-object is an abstraction on top of Angular's reactive forms.
npm install ngx-form-object --saveyarn add ngx-form-objectTo start using ngx-form-object you have to import NgxFormObjectModule into root module of your project.
...
import { AppComponent } from './app.component';
import { NgxFormObjectModule } from 'ngx-form-object';
...
@NgModule({
declarations: [
...
],
imports: [
...
NgxFormObjectModule
...
],
providers: [
...
],
bootstrap: [AppComponent]
})
export class AppModule { }The model will be used to populate the form.
The model must specify which properties are attribute properties (his own properties), which are belongsTo properties, and which properties are hasMany properties. For those puproses Attribute, BelongsTo, and HasMany decorators are exposed.
import { Attribute, HasMany } from 'ngx-form-object';
class User {
@Attribute()
name: string;
@BelongsTo()
depertment: Department;
@HasMany()
cars: Array<Car>
}The task of a specific form object is to manage forms of a specific type.
import { FormObject, FormObjectOptions } from 'ngx-form-object';
export class UserFormObject extends FormObject {
constructor(
public model: User,
protected options: Form
) {
super(model, options);
}
}Form store is created based on the belonging form object. Form object is created out of the model.
const user: User = new User();
const userFormObject: UserFormObject = new UserFormObject(user, null);
const userForm: FormStore = this.formObjectBuilder.create(userFormObject);Import ReactiveFormsModule to the parent module.
Form store can be used in a template in the same way as any other form created by Angular's FormBuilder.
<form [formGroup]="userForm">
<input formControlName="name" />
</form>To save the form (the model), we can simply call .save() on FormStore instance.
userForm.save();
This will search for a service responsible for handling with user model. Form object will search for the service in formObject.serviceMappings[key]. Key in the serviceMappings object represents the model type (model instance name).
constructor(
...,
private injector: Injector,
) {
...
this.serviceMappings = {
user: injector.get(UserService),
};
}In this case, injector is used for injecting the service.
Value in the serviceMappings object represents an instance of a service.
Licensed under the MIT License - see the LICENSE for details.
Maintained and sponsered by Infinum © 2020