Looking at my Angular 8 component, there is a form that allows users to create a post.
The PostModel consists of three properties: a string, a boolean, and a number:
export interface PostModel {
title: string
year: number;
approved: Boolean;
}
The issue arises when I try to create a PostModel from the form values as everything ends up being treated as strings:
let model: PostModel = {
title: this.form.value.title,
approved: this.form.value.approved,
year: this.form.value.year
};
How can I correctly generate a PostModel from the form values while maintaining the correct property types?
Here's the code snippet for the component:
export class PostCreateComponent extends Modal implements OnInit {
form: FormGroup;
constructor(private formBuilder: FormBuilder, private postService: PostService) { }
ngOnInit() {
this.form = this.formBuilder.group({
title: [''],
year: [''],
approved: ['']
});
}
onSubmit() {
if (this.form.valid) {
let model: PostModel = {
title: this.form.value.title,
approved: this.form.value.approved,
year: this.form.value.year
};
// Implement logic to create the post
}
}