Reactive Forms correctly convert Form Value to Model Object

后端 未结 8 1014
刺人心
刺人心 2020-12-04 18:29

While creating a Model Driven Template Reactive forms, when I create model object from Form Value. Then model object is loosing its TYPE.

For a Simple Example:

8条回答
  •  借酒劲吻你
    2020-12-04 18:52

    Change your class definition to

    export class Book {
      constructor(public name: string, public isbn: string) {}
    }
    

    Change your addBook method to

    addBook() {
      const { name, isbn } = this.bookFormGroup.value;
      this.newBook = new Book(name, isbn);
      console.log(this.newBook instanceof Book);
      console.log(this.newBook);
    }
    

提交回复
热议问题