stworzyłem bibliotekę nazywa ngrx-forms że robi dokładnie to, co chcesz. Można go pobrać na KMP poprzez:
npm install ngrx-forms --save
I polecam sprawdzić pełną README na stronie github, ale poniżej znajdziesz kilka przykładów tego, co trzeba zrobić, aby dostać się do góry biblioteki i działa po zainstalowaniu.
importu moduł:
import { StoreModule } from '@ngrx/store';
import { NgrxFormsModule } from 'ngrx-forms';
import { reducers } from './reducer';
@NgModule({
declarations: [
AppComponent,
],
imports: [
NgrxFormsModule,
StoreModule.forRoot(reducers),
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Dodaj stan grupy gdzieś w drzewie państwowej poprzez createFormGroupState
i wywołać formGroupReducer
wewnątrz reduktora:
import { Action } from '@ngrx/store';
import { FormGroupState, createFormGroupState, formGroupReducer } from 'ngrx-forms';
export interface MyFormValue {
someTextInput: string;
someCheckbox: boolean;
nested: {
someNumber: number;
};
}
const FORM_ID = 'some globally unique string';
const initialFormState = createFormGroupState<MyFormValue>(FORM_ID, {
someTextInput: '',
someCheckbox: false,
nested: {
someNumber: 0,
},
});
export interface AppState {
someOtherField: string;
myForm: FormGroupState<MyFormValue>;
}
const initialState: AppState = {
someOtherField: '',
myForm: initialFormState,
};
export function appReducer(state = initialState, action: Action): AppState {
const myForm = formGroupReducer(state.myForm, action);
if (myForm !== state.myForm) {
state = { ...state, myForm };
}
switch (action.type) {
case 'some action type':
// modify state
return state;
default: {
return state;
}
}
}
Expose stan formularz wewnątrz komponentu:
import { Component } from '@angular/core';
import { Store } from '@ngrx/store';
import { FormGroupState } from 'ngrx-forms';
import { Observable } from 'rxjs/Observable';
import { MyFormValue } from './reducer';
@Component({
selector: 'my-component',
templateUrl: './my-component.html',
})
export class MyComponent {
formState$: Observable<FormGroupState<MyFormValue>>;
constructor(private store: Store<AppState>) {
this.formState$ = store.select(s => s.myForm);
}
}
Ustaw kontr stany oli w twoim szablonie:
<form novalidate [ngrxFormState]="(formState$ | async)">
<input type="text"
[ngrxFormControlState]="(formState$ | async).controls.someTextInput">
<input type="checkbox"
[ngrxFormControlState]="(formState$ | async).controls.someCheckbox">
<input type="number"
[ngrxFormControlState]="(formState$ | async).controls.nested.controls.someNumber">
</form>
Używam ngrx, ale nie robię sprawdzania formularza jako takiego. Postanowiłem zostawić NgModel, aby wykonać swoją działalność (mutując ...) bez przeszkód. Używam przycisku przesyłania do wysyłania danych do sklepu i wyzwalania zmian w pozostałej części aplikacji. –
Można użyć sklepu w połączeniu z formularzami opartymi na modelach. Sprawdź ten świetny artykuł na ten temat: http://blog.thoughtram.io/angular/2016/06/22/model-driven-forms-in-angular-2.html –
Poszukuję też miłego przykładu, istnieje mnóstwo tutoriali i blogów na temat formularzy i ngrx, ale nie razem. – Seb