2017-08-13 28 views
1

enter image description here角4.3在與模型

以上結合形式投擲字段的類型錯誤是錯誤消息運行代碼,當我在開發人員工具的控制檯部分中找到。奇怪的是,頁面正常工作,但錯誤信息仍然彈出。我嘗試了很多方法來發現問題,但仍然運氣不佳。請提供一些關於解決這個問題的建議,謝謝。我的HTML代碼顯示中的錯誤是如下:

<!doctype html> 
 
<html> 
 
<body> 
 
    <div class="container"> 
 
     <h1>Movie</h1> 
 
     <form (ngSubmit)="onSubmit()" #movieForm="ngForm"> 
 
      <div class="form-group"> 
 
       <label for="title">Title</label> 
 
       <input type="text" class="form-control" id="title" required [(ngModel)]="model.title" name="title"> 
 
      </div> 
 

 
      <button type="submit" class="btn btn-success" [disabled]="!movieForm.form.valid" >Save</button> 
 
     </form> 
 
    </div> 
 
</body> 
 
</html>

的角度成分的腳本如下:

import { Component } from '@angular/core'; 
import { HttpClient } from '@angular/common/http'; 
import { OnInit } from '@angular/core'; 
import { Movie } from './movie'; 

@Component({ 
    selector: 'movie-form', 
    templateUrl: './movie-form.component.html' 
}) 

export class MovieFormComponent implements OnInit{ 
    model: Movie; 

    constructor(private http: HttpClient) { 

    } 

    ngOnInit(): void { 
     this.http.get('http://localhost:3000/api/movie').subscribe(data => { 
      this.model = new Movie(data['title']); 
     }); 
    } 

    submitted = false; 

    onSubmit(){ 
     this.submitted = true; 
     this.http.post('http://localhost:3000/api/movie', { 
       "title": this.model.title 
      }).subscribe(data => { 
     }); 
    } 
} 
+0

在TS中聲明'model = new Movie()'。無論如何,您需要初始化'model',以便它在響應到達之前不會被定義爲':)' – Alex

+0

[Angular 2數據綁定(無法讀取未定義屬性'proposta \ _usuario']](https:/ /stackoverflow.com/questions/42581691/angular-2-data-binding-cannot-read-property-proposta-usuario-of-undefined) – Alex

回答