为什么这些结果不一样?
why are these results not the same?
这是我的控制台的输出:
"_diasInhabilesCampos": [
{
"fecha": "2021-10-11T05:00:00.000Z"
},
{
"fecha": "2021-10-11T05:00:00.000Z"
}
],
HTML
<div [formGroupName]="index"
*ngFor="let item of formularioVertical.get('paso4._diasInhabilesCampos').controls, let index = index">
<mat-form-field [ngClass]="formFieldHelpers" class="flex-auto mt-4 w-full">
<input matInput [min]="fechasMin.min" [max]="fechasMin.max" [matDatepicker]="picker1"
placeholder="Escoje una fecha" [formControlName]="'fecha'" (dateChange)="dateSelect($event,index)">
<mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle>
<mat-datepicker #picker1></mat-datepicker>
</mat-form-field>
<div class="flex justify-start">
<button class="px-8" mat-flat-button [color]="'accent'" type="button" (click)="deleteDiasInhabiles(index)">
Eliminar día
</button>
</div>
</div>
代码 TS
dateSelect(e: any, index: number): void {
const _dias = this.formularioVertical.get('paso4._diasActivo').value;
if (e.value !== '') {
const fecha = e.value._d;
const dia = moment(fecha).day();
if (_dias.indexOf(dia.toString()) === - 1) {
this.formularioVertical.get('paso4._diasInhabilesCampos.' + index + '.fecha').setValue('');
this.formularioVertical.get('paso4._diasInhabilesCampos.' + index + '.fecha').updateValueAndValidity();
this.formularioVertical.get('paso4._diasInhabilesCampos').updateValueAndValidity();
} else {
const _fechas = this.formularioVertical.get('paso4._diasInhabilesCampos').value;
const clone = [..._fechas];
if (_fechas.length > 1) {
clone.pop();
const _encontrado = clone.find(element => element.fecha === e.value._d);
console.log(clone[0].fecha === e.value._d); <---- *OUTPUT: FALSE
console.log(_encontrado === undefined); <---- *OUTPUT: TRUE
if (_encontrado === undefined) {
this.formularioVertical.get('paso4._diasInhabilesCampos.' + index + '.fecha').setValue(fecha);
this.formularioVertical.get('paso4._diasInhabilesCampos.' + index + '.fecha').updateValueAndValidity();
this.formularioVertical.get('paso4._diasInhabilesCampos').updateValueAndValidity();
} else {
this.formularioVertical.get('paso4._diasInhabilesCampos.' + index + '.fecha').setValue('');
this.formularioVertical.get('paso4._diasInhabilesCampos.' + index + '.fecha').updateValueAndValidity();
this.formularioVertical.get('paso4._diasInhabilesCampos').updateValueAndValidity();
}
} else {
this.formularioVertical.get('paso4._diasInhabilesCampos.' + index + '.fecha').setValue(fecha);
this.formularioVertical.get('paso4._diasInhabilesCampos.' + index + '.fecha').updateValueAndValidity();
this.formularioVertical.get('paso4._diasInhabilesCampos').updateValueAndValidity();
}
}
}
}
我正在使用 MomentJS 包以及动态 Angular 反应形式。
我有一个名为“添加”的按钮,这会在我的反应式表单中创建一个额外的字段,该字段允许我根据我的代码选择日期,首先它会检查日期是否对应于工作周的启用日,这部分工作没有问题,我的问题是当我想看看我输入的日期是否不重复时,我在我的数组中使用查找,奇怪的是我输入了相同的日期,但它没有检测到它是相同的,虽然我的控制台显示相反,这是什么原因,提前非常感谢您的帮助。
兄弟们别担心,我的坏蛋,这只驴在比较它们之前忘记格式化日期,我已经修复了,无论如何谢谢。
我只需在函数的每一站更改此短语即可。
setValue(fecha) ---> setValue(moment(fecha).format())
这是我的控制台的输出:
"_diasInhabilesCampos": [
{
"fecha": "2021-10-11T05:00:00.000Z"
},
{
"fecha": "2021-10-11T05:00:00.000Z"
}
],
HTML
<div [formGroupName]="index"
*ngFor="let item of formularioVertical.get('paso4._diasInhabilesCampos').controls, let index = index">
<mat-form-field [ngClass]="formFieldHelpers" class="flex-auto mt-4 w-full">
<input matInput [min]="fechasMin.min" [max]="fechasMin.max" [matDatepicker]="picker1"
placeholder="Escoje una fecha" [formControlName]="'fecha'" (dateChange)="dateSelect($event,index)">
<mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle>
<mat-datepicker #picker1></mat-datepicker>
</mat-form-field>
<div class="flex justify-start">
<button class="px-8" mat-flat-button [color]="'accent'" type="button" (click)="deleteDiasInhabiles(index)">
Eliminar día
</button>
</div>
</div>
代码 TS
dateSelect(e: any, index: number): void {
const _dias = this.formularioVertical.get('paso4._diasActivo').value;
if (e.value !== '') {
const fecha = e.value._d;
const dia = moment(fecha).day();
if (_dias.indexOf(dia.toString()) === - 1) {
this.formularioVertical.get('paso4._diasInhabilesCampos.' + index + '.fecha').setValue('');
this.formularioVertical.get('paso4._diasInhabilesCampos.' + index + '.fecha').updateValueAndValidity();
this.formularioVertical.get('paso4._diasInhabilesCampos').updateValueAndValidity();
} else {
const _fechas = this.formularioVertical.get('paso4._diasInhabilesCampos').value;
const clone = [..._fechas];
if (_fechas.length > 1) {
clone.pop();
const _encontrado = clone.find(element => element.fecha === e.value._d);
console.log(clone[0].fecha === e.value._d); <---- *OUTPUT: FALSE
console.log(_encontrado === undefined); <---- *OUTPUT: TRUE
if (_encontrado === undefined) {
this.formularioVertical.get('paso4._diasInhabilesCampos.' + index + '.fecha').setValue(fecha);
this.formularioVertical.get('paso4._diasInhabilesCampos.' + index + '.fecha').updateValueAndValidity();
this.formularioVertical.get('paso4._diasInhabilesCampos').updateValueAndValidity();
} else {
this.formularioVertical.get('paso4._diasInhabilesCampos.' + index + '.fecha').setValue('');
this.formularioVertical.get('paso4._diasInhabilesCampos.' + index + '.fecha').updateValueAndValidity();
this.formularioVertical.get('paso4._diasInhabilesCampos').updateValueAndValidity();
}
} else {
this.formularioVertical.get('paso4._diasInhabilesCampos.' + index + '.fecha').setValue(fecha);
this.formularioVertical.get('paso4._diasInhabilesCampos.' + index + '.fecha').updateValueAndValidity();
this.formularioVertical.get('paso4._diasInhabilesCampos').updateValueAndValidity();
}
}
}
}
我正在使用 MomentJS 包以及动态 Angular 反应形式。
我有一个名为“添加”的按钮,这会在我的反应式表单中创建一个额外的字段,该字段允许我根据我的代码选择日期,首先它会检查日期是否对应于工作周的启用日,这部分工作没有问题,我的问题是当我想看看我输入的日期是否不重复时,我在我的数组中使用查找,奇怪的是我输入了相同的日期,但它没有检测到它是相同的,虽然我的控制台显示相反,这是什么原因,提前非常感谢您的帮助。
兄弟们别担心,我的坏蛋,这只驴在比较它们之前忘记格式化日期,我已经修复了,无论如何谢谢。
我只需在函数的每一站更改此短语即可。
setValue(fecha) ---> setValue(moment(fecha).format())