首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ngrx-form,ngrx-store,以编程方式更改值

ngrx-form,ngrx-store,以编程方式更改值
EN

Stack Overflow用户
提问于 2022-04-25 09:09:58
回答 1查看 605关注 0票数 0

我有下面的reducer.ts

代码语言:javascript
复制
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中的输入字段:

代码语言:javascript
复制
<input [ngrxFormControlState]="doctransFormState.controls.erstellt">

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

我尝试了以下几点:

代码语言:javascript
复制
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/user-guide/updating-the-state/#setting-the-value

有人能帮忙吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-25 11:36:41

终于来了。明白了:

代码语言:javascript
复制
this.store.dispatch(new SetValueAction('doctransForm.erstellt', 'newValue3'));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71997048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档