首页 文章

如何获取数据并在angular2中订阅它

提问于
浏览
0

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

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 回答

  • 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

相关问题