如何获取数据并在angular2

时间:2016-08-20 06:31:25

标签: angular angular2-observables

我正在试图获得结果,并用可观察的东西来解决它,但我无法得到它可以请一些人帮助我。

 import { Injectable } from '@angular/core';
 import {Component} from '@angular/core';
 import {Http, Response, Headers} from '@angular/http';
 import {Observable} from 'rxjs/Observable';
 import { IStudent1 } from '../interface';
 import { IStudent2 } from '../interface';
 import {ROUTER_DIRECTIVES} from '@angular/router';
 import {GetSocietyList} from './service'
  import {FORM_DIRECTIVES, FormBuilder, FormGroup, Validators, REACTIVE_FORM_DIRECTIVES} from '@angular/forms';

  @Component({
      selector: 'Search',
      templateUrl:  './components/search/search.html',
      directives: [ ROUTER_DIRECTIVES, FORM_DIRECTIVES,    REACTIVE_FORM_DIRECTIVES],
       providers : [GetSocietyList],
       inputs : ['form']
    })

    export class Search {
    property:any = 'Add';
     data: string;
     form: FormGroup;
    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 }).map(res=> res.json());}


}

我正在试图获得结果,并用可观察的东西来解决它,但我无法得到它可以请一些人帮助我。

1 个答案:

答案 0 :(得分:1)

您可以使用map等任何功能操作符来获取响应。

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 }).map(res=> res.json());

`

然后在您的组件/指令中,您可以subscribe获取它。

constructor(private service:SomeHttpService) {
}

callingGetService() {
  this.service.edit('id').subscribe();
}

看看here