2016-08-20 2 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를 기록합니다. 가능성이 가장 높은 작품을 가져 오는

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

그래서 데이터, 당신은 너무 열심히하여 로그인을 시도하고 있습니다 : 당신은 데이터를 볼 끝까지 요청을 기다릴 필요가있다.

관련 문제