Angular UpdateFormValue操作不更新FormControl值
我正在使用带有表单插件的ngxs作为我的Nativescript应用程序的状态管理 我想要的是手动更新表单控件值。我创建了以下表单:Angular UpdateFormValue操作不更新FormControl值,angular,nativescript,ngxs,Angular,Nativescript,Ngxs,我正在使用带有表单插件的ngxs作为我的Nativescript应用程序的状态管理 我想要的是手动更新表单控件值。我创建了以下表单: this.form = this.fb.group({ type: [''], }) 我在我的html上呈现它,如下所示: <StackLayout [formGroup]="form" ngxsForm="transactionHistory.filterForm" class="form"> <FlexboxLayout>
this.form = this.fb.group({
type: [''],
})
我在我的html上呈现它,如下所示:
<StackLayout [formGroup]="form" ngxsForm="transactionHistory.filterForm" class="form">
<FlexboxLayout>
<DatePickerField hint=" Select from " width="50%" class="fas date-select"></DatePickerField>
<DatePickerField hint=" Select to" width="50%" class="fas date-select"></DatePickerField>
</FlexboxLayout>
<FlexboxLayout>
<Button (tap)="onChooseType()" [text]="selectedType + ' '" class="dropdown-btn fas"></Button>
<Button text="All status " class="dropdown-btn fas"></Button>
</FlexboxLayout>
<FlexboxLayout>
<Button text="All Payment Options " class="dropdown-btn fas po" width="100%"></Button>
</FlexboxLayout>
<FlexboxLayout justifyContent="flex-end">
<Button text="Reset Filters" class="btn-reset"></Button>
<Button (tap)="submit()" text="Apply" class="btn-apply"></Button>
</FlexboxLayout>
</StackLayout>
但是,状态的更改不会更新我的FormGroup上的值。太奇怪了。是的,你的上一个代码是正确的。由于
路径
属性与模型
连接,因此它成为transactioHistory.FilterPerform.model
(状态名称、对象、模型)。你能提供一些关于stackblitz的简单例子吗?我无法重现您的问题,也无法在nativescript应用程序中运行:)@Shadow nativescript不支持stackblitz。我开始认为这与nativescript有关:(你能提出一个大胆的猜测吗?:)不,我不是要创建nativescript应用程序,我是要创建类似于你的示例的东西。尝试收听valueChanges
流,查看在执行UpdateFormValue
操作后是否发生了某些事情handled@Shadow好的,我想我必须测试一下:)谢谢!我的自定义表单控件(不是NativeScript)也有同样的问题,原因是writeValue
函数中缺少逻辑(我只是忘了为输入元素添加属性更新,所以看不到更改)。
export class TransactionHistoryStateModel {
transactions: PaginatedList<Transaction>;
loading: boolean;
filterForm: any;
availableTypes: any;
}
@State({
name: 'transactionHistory',
defaults: {
transactions: null,
loading: false,
filterForm: {
model: undefined,
dirty: false,
status: '',
errors: {},
selectedType: 'All Types',
},
availableTypes: TRANSACTION_HISTORY_TYPE_MAPPING
}
})
export class TransactionHistoryState {
constructor(private client: HttpClient, private store: Store) {
}
@Action(HandleChosenType)
handleChosenType({ setState, getState, dispatch }: StateContext<TransactionHistoryStateModel>, { selectedItem }: HandleChosenType) {
const state = getState();
const filterForm = state.filterForm;
filterForm.selectedType = selectedItem.text;
setState({
...state,
filterForm
});
return dispatch(new UpdateFormValue({
value: 'WAH' ,
path: 'transactionHistory.filterForm.type'
}));
}
}
this.store.dispatch([
new UpdateFormValue({
value: [ {type: 'HAHA' }],
path: 'transactionHistory.filterForm'
}),
]);