NGRX形式,NGRX商店,以编程方式更改值

发布于 2025-01-23 22:18:06 字数 1997 浏览 0 评论 0原文

我有以下reducer.ts

export const FORM_ID = 'doctransForm';

export interface FormValue {
  erstellt: string;
}

export const INITIAL_STATE = createFormGroupState<FormValue>(FORM_ID, {
  erstellt: ''
});

export interface State {
  entities: DoctransEntity[];
  errorMessage: string;
  loading: DataState;
  formState: FormGroupState<FormValue>;
}

export const doctransInitialState: State = {
  entities: [],
  errorMessage: null,
  loading: DataState.LOADED_STATE,
  formState: INITIAL_STATE
}

const _doctransReducer = createReducer(
  doctransInitialState,
  onNgrxForms(),
  on(formValueChange, (state, { type, ...update }) => ({ ...state, ...update })),
  on(DoctransActions.resetForm, (state, action) => ({ ...state, formState: INITIAL_STATE })),
  on(DoctransActions.setErstellt, (state, action) => ({ ...state, formState: { ...state.formState, value: { ...state.formState.value, erstellt: action.erstellt} }})),
);

export function doctransReducer(state: State, action: Action) {
  return _doctransReducer(state, action);
}

在html中的输入场:

<input [ngrxFormControlState]="doctransFormState.controls.erstellt">

现在我只想以编程方式更改值。

我尝试了以下内容:

this.store.dispatch(setErstellt({erstellt: 'lala test'}));

    const control = createFormControlState<string>('doctransFormState.controls.erstellt', '');
    const updatedControl = setValue('new Value1')(control);
    const updatedControlUncurried = setValue(control, 'newValue2');
    const updatedControlViaAction = formStateReducer(control, new SetValueAction(control.id, 'newValue3'));

但是没有任何作用。看来我不明白如何从这里使用setValue: https://ngrx-forms.readthedocs.io/en/master/master/user-guide/updating-the-state/#setting-the-the-value

有人可以帮忙吗?

I have the following reducer.ts

export const FORM_ID = 'doctransForm';

export interface FormValue {
  erstellt: string;
}

export const INITIAL_STATE = createFormGroupState<FormValue>(FORM_ID, {
  erstellt: ''
});

export interface State {
  entities: DoctransEntity[];
  errorMessage: string;
  loading: DataState;
  formState: FormGroupState<FormValue>;
}

export const doctransInitialState: State = {
  entities: [],
  errorMessage: null,
  loading: DataState.LOADED_STATE,
  formState: INITIAL_STATE
}

const _doctransReducer = createReducer(
  doctransInitialState,
  onNgrxForms(),
  on(formValueChange, (state, { type, ...update }) => ({ ...state, ...update })),
  on(DoctransActions.resetForm, (state, action) => ({ ...state, formState: INITIAL_STATE })),
  on(DoctransActions.setErstellt, (state, action) => ({ ...state, formState: { ...state.formState, value: { ...state.formState.value, erstellt: action.erstellt} }})),
);

export function doctransReducer(state: State, action: Action) {
  return _doctransReducer(state, action);
}

An input-field in html:

<input [ngrxFormControlState]="doctransFormState.controls.erstellt">

Now i just want to simply change the value programmatically.

I tried the following:

this.store.dispatch(setErstellt({erstellt: 'lala test'}));

    const control = createFormControlState<string>('doctransFormState.controls.erstellt', '');
    const updatedControl = setValue('new Value1')(control);
    const updatedControlUncurried = setValue(control, 'newValue2');
    const updatedControlViaAction = formStateReducer(control, new SetValueAction(control.id, 'newValue3'));

But nothing works. It seems i dont understand how to use the setValue from here: https://ngrx-forms.readthedocs.io/en/master/user-guide/updating-the-state/#setting-the-value

Can anyone help out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海未深 2025-01-30 22:18:06

最后。知道了:

this.store.dispatch(new SetValueAction('doctransForm.erstellt', 'newValue3'));

Finally. Got it:

this.store.dispatch(new SetValueAction('doctransForm.erstellt', 'newValue3'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文