2016-08-20 38 views
0

我正在嘗試獲取編輯目的的詳細信息,但我不確定如何使用observables來執行此操作,但有些人可以幫助我解決問題,這是編輯功能。如何在angular2中執行http調用

@Component({ 
     selector: 'Search', 
     templateUrl: './components/search/search.html', 
     directives: [REACTIVE_FORM_DIRECTIVES], 
     providers : [GetSocietyList], 

    }) 

     export class Search { 
     property:any = 'Add'; 
     data: string; 
     form:any; 
     prop: string = 'connenct'; 
     details: IStudent1[]; 
     details1: IStudent2[]; 

    constructor(fbld: FormBuilder,public http: Http,private _profileservice:GetSocietyList) { 
    this.details = []; 
    this.http = http; 
    this._profileservice.getSocietyList() 
     .subscribe(details => this.details = details); 
     console.log(this.details); 
    this.form = fbld.group({ 
     name: [''], 
     age: ['', Validators.required], 
     class: ['', Validators.required], 
     grade: ['', Validators.required] 

    }); 

    } 

    edit(id): any { 
    //console.log(id); 
    this.property = 'update'; 
    var headers = new Headers(); 
    headers.append('Content-Type', 'application/x-www-form-urlencoded') 
    this.http.get('http://localhost/a2server/index.php/profile/editprofiledb/' + id, { headers: headers }) 
     .subscribe(response => { 
      if (response.json().error_code == 0) { 
       this.details = <IStudent2[]>response.json().data; 

      } else { 
       this.details = <IStudent2[]>response.json().data; 


     } 

    }  )} 

我試圖獲得編輯目的的細節,但我不知道如何與觀測做一些能夠幫助一個我的問題是與編輯功能。

+0

那麼,這是什麼問題? –

+0

我在這裏有一個教程,我在角2中涵蓋不同的http場景http://www.syntaxsuccess.com/viewarticle/angular-2.0-and-http – TGH

回答

1
this._profileservice.getSocietyList() 
    .subscribe(details => this.details = details); 
    console.log(this.details); 

這將始終記錄undefined,因爲您在訂閱observable後正在登錄。你需要等待請求的完成來查看數據:

this._profileservice.getSocietyList() 
    .subscribe(details => { 
     this.details = details; 
     console.log(this.details); 
    }); 

所以你的數據抓取最有可能的工作,你只是想記錄它太急切。