2017-08-09 181 views
1

我要使用角度4提交表單到django後端api,但當單擊保存按鈕時沒有任何動作發生。我監控的後端並沒有任何傳入請求我無法揣摩出我的代碼出錯,下面我的代碼:發送POST請求 - angular4

addEmployee.component.ts:

import { Employee } from './../employee'; 
import { AddEmployeeService } from './add-employee.service'; 
import { Component, OnInit } from '@angular/core'; 
import { DataService } from './../../services/data.service'; 

@Component({ 
    selector: 'app-addemployee', 
    templateUrl: './addemployee.component.html', 
    styleUrls: ['./addemployee.component.css'] 
}) 
export class AddemployeeComponent implements OnInit { 
    jobs: Array<any>; 
    mahalyas: Array<any>; 
    courses: Array<any>; 
    employee: Array<any>; 
    constructor(private dataService: DataService, private AddEmployeeService: AddEmployeeService) { } 

    ngOnInit() { 
     this.dataService.getAllMahaleyahs().subscribe(
     data => this.mahalyas = data 
    ); 
     this.dataService.getAllCourses().subscribe(
     data => this.courses = data 

    ); 
     this.dataService.getAllJobs().subscribe(
     data => this.jobs = data 
    ); 
    } 
    onSave(){ 
    this.dataService.saveEmployee(this.employee); 
    } 

} 

Employee.ts:

export class Employee 
{ 
    constructor 
    ( 
    public id: number, 
    public name: string, 
    public grade: string, 
    public maritial_status: string, 
    public number_of_children: string, 
    public spouse_work_place: string, 
    public parent_status: string, 
    public helth_status: string, 
    public date_of_transfer_to_mahaleyah: string, 
    public address: string, 
    public mobile_number: string, 
    public employment_type: string, 
    public qualification: string, 
    public extra_qualification: string, 
    public note: string, 
    public job: string, 
    public previouse_mahaleyah: string, 
    public courses: string){} 

} 

addEmployee.component.html:

<div class="main-content"> 
    <div class="row"> 
     <div class="col-sm-8 col-xs-offset-2"> 
     <div class="panel panel-default"> 
      <div class="panel-heading clearfix"> 
       <h4><strong> إضافة موظف</strong></h4> 
      </div> 
      <div class="panel-body"> 
       <form class="form-inline" ng-submit="onSave()"> 
        <div class="list-group"> 
        <div class="list-group-item"> 
         <label class="list-group-item-text bottom-up">الإسم:</label> 
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
         <input type="text" class="form-group form-control" name="name" size="40"> 
        </div> 
        <div class="list-group-item"> 
         <label class="list-group-item-text bottom-up">نوع التعيين:</label> 
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
         <select class="form-group form-control" name="employment_type"> 
          <option value="معين">معين</option> 
          <option value="متعاون">متعاون</option> 
         </select> 
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
         <label class="list-group-item-text bottom-up">الحالة الإجتماعية:</label> 
         &nbsp; 
         <select class="form-control" name="maritial_status"> 
          <option value="متزوج">متزوج</option> 
          <option value="اعزب">اعزب</option> 
          <option value="مطلق">مطلق</option> 
          <option value="ارمل">ارمل</option> 
         </select> 
         &nbsp;&nbsp;&nbsp; 
         <label class="list-group-item-text bottom-up">الوظيفة:</label> 
         &nbsp;&nbsp;&nbsp;&nbsp; 
         <select class="form-control" name="job"> 
          <option *ngFor="let job of jobs" value="{{job.value}}">{{job.name}}</option> 
         </select> 
        </div> 
        <div class="list-group-item"> 
         <label class="list-group-item-text bottom-up">المؤهل:</label> 
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
         <input name="qualification" class="form-control" type="text"> 
         &nbsp;&nbsp; 
         <label class="list-group-item-text bottom-up">المؤهل الإضافي:</label> 
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
         <input name="extra_qualification" class="form-control" type="text"> 
        </div> 
        <div class="list-group-item"> 
         <label class="list-group-item-text bottom-up">العنوان:</label> 
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
         <input name="address" class="form-control" type="text"> 
         &nbsp;&nbsp;&nbsp; 
         <label class="list-group-item-text bottom-up">المحلية السابقة:</label> 
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 
         <select class="form-control" name="previouse_mahaleyah"> 
          <option *ngFor="let mahaleyah of mahalyas" value="{{mahaleyah.id}}">{{mahaleyah.name}}</option> 
         </select> 
        </div> 
        <div class="list-group-item"> 
         <label class="list-group-item-text bottom-up">تاريخ النقل للمحلية:</label> 
         <input type="text" class="form-control" placeholder="mm/dd/yyyy" name="date_of_transfer_to_mahaleyah"> 
         &nbsp;&nbsp;&nbsp; 
         <label class="list-group-item-text bottom-up">الحالة الصحية:</label> 
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
         <input type="text" class="form-control" name="health_status"> 
        </div> 
        <div class="list-group-item"> 
         <label class="list-group-item-text bottom-up">عدد الأطفال:</label> 
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
         <input type="text" class="form-control" name="number_of_children"> 
         &nbsp;&nbsp; 
         <label class="list-group-item-text bottom-up">مكان عمل الزوج:</label> 
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
         <input type="text" class="form-control" name="spouse_work_place"> 
        </div> 
        <div class="list-group-item"> 
         <label class="list-group-item-text bottom-up">رقم الهاتف:</label> 
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
         <input type="text" class="form-control"> 
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
         <label class="list-group-item-text bottom-up">الدورات: </label> 
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
         <select class="form-control" name="courses"> 
          <option *ngFor="let course of courses" value="{{course.id}}">{{course.name}}</option> 
         </select> 
        </div> 
        <div class="list-group-item"> 
         <label class="list-group-item-text bottom-up">حالة الوالدين:</label> 
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
         <textarea type="text" class="form-control" name="parent_status"></textarea> 
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
         <label class="list-group-item-text bottom-up">ملاحظات:</label> 
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
         <textarea type="text" class="form-control" name="note"></textarea> 
        </div> 
        </div> 
        <div class="new-employee-butons"> 
        <input type="submit" class="btn btn-md btn-success" value="save" (click)="onSave()"> 
        <button class="btn btn-md btn-danger "> 
        close 
        </button> 
        </div> 
       </form> 
      </div> 
     </div> 
     <!-- End of Pnel --> 
     </div> 
    </div> 
</div> 

data.service.ts:

import { Injectable, Input } from '@angular/core'; 
import { Http, Headers } from '@angular/http'; 
import { Kg } from '../kgs/kg'; 


import 'rxjs/add/operator/map'; 

@Injectable() 
export class DataService { 

    baseUrl: string = 'http://localhost:8000/kgms/'; 
    constructor(private http: Http) { } 

/* Getters starts*/ 
    public getAllEmployees(){ 
    return this.http.get(this.baseUrl+'employees/') 
     .map(res => res.json()); 
    } 

    public getAllKgs(){ 
    return this.http.get(this.baseUrl+'kgs/') 
     .map(res => res.json()); 
    } 

    public getAllStudents(){ 
    return this.http.get(this.baseUrl+'students/') 
     .map(res => res.json()); 
    } 

    public getAllAssessments(){ 
    return this.http.get(this.baseUrl+'asses/') 
     .map(res => res.json()); 
    } 

    public getAllMahaleyahs(){ 
    return this.http.get(this.baseUrl+'mahaleyahs/') 
     .map(res => res.json()); 
    } 

    public getAllCourses(){ 
    return this.http.get(this.baseUrl+'courses/') 
     .map(res => res.json()); 
    } 
    public getAllJobs(){ 
    return this.http.get(this.baseUrl+'jobs/') 
     .map(res => res.json()); 
    } 

/* Getters Ends */ 

/* Setters starts */ 

    saveEmployee(employee){ 
    const headers = new Headers({'Content-type': 'application/json'}); 
    return this.http.post('http://localhost:8000/kgms/', employee, {headers: headers}); 
    } 



public addKg(kg:Kg){ 

    return this.http.put(this.baseUrl+'kgs/', kg, { 
    }) 
    .map(res => res.json()); 
} 

/* Setters Ends */ 






} 

我感謝所有幫助和建議

+0

您是否獲得在您的瀏覽器控制檯的任何錯誤? –

+0

實際上沒有,在控制檯上沒有任何錯誤 –

+0

你必須'訂閱'到'post'方法。 –

回答

3

http.post調用不會返回,直到你打電話.subscribe()

既然你正在重返saveEmployeeObservable,您可以添加到.subscribe()this.dataService.saveEmployee(this.employee)

末然後,它會看起來像:

onSave(){ 
    this.dataService.saveEmployee(this.employee).subscribe((response) => { 
    // do something with 'response' 
    }); 
} 
+0

我會嘗試這個,我會回來的結果,謝謝 –

+0

非常感謝,這幫助我得到表單提交到後端。它返回一個錯誤,但它最終響應。 –