在排序数组后使用setValue形成控件时出错



我有这个错误:

错误错误:必须为索引1处的表单控件提供一个值。

我正在用Angular 9做一个应用程序,问题出在Typescript上。

以下代码正在工作:

this.registros.controls.arrayProcedimientos.setValue(convert.arrayProcedimientos);

但如果我对数组进行排序,然后执行setValue,我不知道会发生什么,但不起作用,例如:

convert.arrayProcedimientos.sort(function(a,b){
return a.fechaInicioProcedimiento > b.fechaInicioProcedimiento;
});
//setValue of the sorted array
this.registros.controls.arrayProcedimientos.setValue(convert.arrayProcedimientos);

psd:我试着做了一个patchValue((,它没有抛出错误,但问题是对象是一个数组数组。因此,在唯一字段上执行patchValue效果很好,但在子数组上却很麻烦。

感谢您的关注和帮助,如果您需要一些照片或其他什么,请随时询问。

伟大的社区:(

我认为首先你需要修复它们的排序方式,不确定它对你来说是如何工作的。它一定会在排序时给您带来编译时错误。

而不是这个

convert.arrayProcedimientos.sort(function(a,b){
return a.fechaInicioProcedimiento > b.fechaInicioProcedimiento;
});

使用此

convert.arrayProcedimientos.sort(function(a,b){
return a.fechaInicioProcedimiento - b.fechaInicioProcedimiento;
});

然后

this.registros.controls.arrayProcedimientos.setValue(convert.arrayProcedimientos);

应该起作用。

以下是我要求您更改排序语法的原因TypeScript排序数组

最新更新