Reactive Forms correctly convert Form Value to Model Object

后端 未结 8 1015
刺人心
刺人心 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:42

    This constructor will work with any type and will assign any matching filed.

    export class Book {
      public constructor(init?: Partial) {
            Object.assign(this, init);
        }
    }
    

    So you will be able to do this:

    this.newBook = new Book(this.bookFormGroup.value);
    

    This will safe you a lot of work if the Book class will have any change in future and became bigger.

提交回复
热议问题