2017-03-03 52 views
1

屬性「後」我對原始異常:無法讀取的不確定在角2

的問題無法在提交表單讀取屬性在角2

的不確定的「後」,一個是所謂的,這裏的功能是代碼

onSubmit() { 
    console.log("onsubmit->", this.addForm.value); 
    let addArray = this.addForm.value; 
    this._employeeservice.addEmployeeCollection(addArray) 
    .subscribe(sample => { 
     this.dbemp.push(sample); 
    }); 
    this.addForm.reset(); 
    this.SubmitToast(); 
} 

addEmployeeCollection()代碼是在這裏

addEmployeeCollection(addArray: EmployeeSchema) { 
    let url: string = Constants.DOMAIN + Constants.CREATE_EMPLOYEE_ROUTE; 
    console.log('addArray at emplyee service', addArray) 
    var body = addArray; 
    let headers = new Headers(); 
    headers.append('Content-Type', 'application/json'); 
    let options = { 
    headers: headers 
    }; 
    let token = localStorage.getItem('realtoken'); 
    options.headers.set('Authorization', ` ${token}`); 
    return this.http.post(url, body, options).map(res => res.json()).catch(this._errorHandler); 
} 
+0

http://stackoverflow.com/questions/34450410/typeerror- can not-read-property-post-of-undefined-in-null –

+0

請提供您實例化this.http服務的代碼。 – eminlala

+0

Http服務沒有被注入。 –

回答

0

一般來說,一旦您收到您的成功電話後,清空您的表格會更安全。所以,試試這個:

onSubmit() { 
    console.log("onsubmit->", this.addForm.value); 
    let addArray = this.addForm.value; 
    this._employeeservice.addEmployeeCollection(addArray) 
    .subscribe(sample => { 
     this.dbemp.push(sample); 
     this.addForm.reset(); //move this in the subscribe area 
     this.SubmitToast(); 
    }); 
} 
1

根據意見,我們瞭解到,HTTP沒有被正確注射,造成this.httpundefined

相反標記,像這樣在服務中的http:

export class EmployeeService { 
    public http: Http; 
} 

應該在構造函數中注入:

export class EmployeeService { 
    constructor(public http: Http) { } 
}