【问题标题】:ngrx-form, ngrx-store, change value programmaticallyngrx-form,ngrx-store,以编程方式更改值
【发布时间】:2022-04-25 19:36:41
【问题描述】:

我有以下 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'));

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

任何人都可以帮忙吗?

【问题讨论】:

    标签: angular ngrx-store ngrx-forms


    【解决方案1】:

    最后。知道了:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-30
      • 1970-01-01
      • 2021-03-04
      • 1970-01-01
      • 1970-01-01
      • 2017-04-15
      • 1970-01-01
      • 2017-12-27
      相关资源
      最近更新 更多