在创建模型驱动模板Reactive表单时,我从Form Value创建模型对象时.然后模型对象失去其TYPE.
对于一个简单的例子:
模特班书:
export class Book {
public name: string;
public isbn: string;
}
Run Code Online (Sandbox Code Playgroud)
零件:
@Component({
selector: 'app-book',
templateUrl: './book.component.html',
styleUrls: ['./book.component.css']
})
export class BookComponent implements OnInit {
bookFormGroup: FormGroup;
private newBook: Book = new Book();
constructor(private fb: FormBuilder) {
this.bookFormGroup = this.fb.group({
name: new FormControl(''),
isbn: new FormControl('')
});
}
ngOnInit() {
}
addBook() {
console.log('submit');
this.newBook = <Book> this.bookFormGroup.value;
console.log(this.newBook instanceof Book);
console.log(this.newBook);
}
}
Run Code Online (Sandbox Code Playgroud)
HTML:
<form [formGroup]="bookFormGroup" (ngSubmit)="addBook()">
<input type="text" formControlName="name" >
<input type="text" formControlName="isbn" …Run Code Online (Sandbox Code Playgroud)