/example-4.ts Secret
Created
November 4, 2019 22:08
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare const stringDateValidator: ValidatorFn; | |
declare const stringToDate: (value: string) => Date | null; | |
declare const dateToString: (value: Date | null) => string; | |
class ExampleFourComponent implements OnInit { | |
inputControl = new FormControl('', { | |
validators: stringDateValidator, | |
}); | |
dateControl = new FormControl<Date | null>(null); | |
ngOnInit() { | |
this.inputControl.events | |
.pipe( | |
map(event => { | |
if (event.type === 'StateChange' && event.changes.has('value')) { | |
const changes = new Map(event.changes); | |
changes.set('value', stringToDate(changes.get('value'))); | |
return { | |
...event, | |
changes, | |
}; | |
} | |
return event; | |
}), | |
) | |
.subscribe(this.dateControl.source); | |
this.dateControl.events | |
.pipe( | |
map(event => { | |
if (event.type === 'StateChange' && event.changes.has('value')) { | |
const changes = new Map(event.changes); | |
changes.set('value', dateToString(changes.get('value'))); | |
return { | |
...event, | |
changes, | |
}; | |
} | |
return event; | |
}), | |
) | |
.subscribe(this.inputControl.source); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment