0

我将mydatepicker与以下自制帮助类一起使用:

import {AbstractControl, FormBuilder} from '@angular/forms';

export const dateRangeValidator = (control: AbstractControl): {[key: string]: boolean} => {
	const start = control.get('start');
	const end = control.get('end');
	if (!start || !end) {
		return null;
	}
	if (!start.value || !end.value) {
		return {required: true};
	}
	return start.value.jsdate < end.value.jsdate ? null : {invalid: true};
};

export const createDateRangeGroup = (fb: FormBuilder) => {
	return fb.group({
		start: null,
		end: null
	}, { validator: dateRangeValidator});
};

export const setDateRangeGroupDates = (dates: AbstractControl, start: Date, end: Date) => {
	dates.patchValue({
		start: {
			date: {
				year: start.getFullYear(),
				month: start.getMonth() + 1,
				day: start.getDate()
			}
		},
		end: {
			date: {
				year: end.getFullYear(),
				month: end.getMonth() + 1,
				day: end.getDate()
			}
		}
	});
};

createDateRangeGroup在组件中使用如下:

this.formGroup = this.fb.group({
  dates: createDateRangeGroup(this.fb)
});
setDateRangeGroupDates(this.formGroup.get('dates'), new Date(2017, 10, 1), new Date(2017, 10, 10));

我可以在屏幕上看到日期设置正确。但是,当我尝试它时,console.log(this.formGroup.get('dates'));它清楚地显示了一个错误,{invalid: true}即使我只是从下拉列表中重新选择完全相同的日期,该组也会自动生效。

我不做什么?

4

0 回答 0