角度为2的可观察量不起作用(Http方法)

时间:2017-04-30 18:04:39

标签: javascript angular http observable angular2-services

我的服务有问题。我有一个服务,它从HTTP模块获得一个JSON并填写一个类' olders'用它。但是,当我打电话给我的服务时,它并没有做任何事情,而我的班级老板仍然没有结束...... 如果我在yearbook.component.html中制作一个* ngFor指令,我什么都没得到...... 我认为我的问题来自我的服务,但我不知道错误在哪里...... 即使我把console.log放在subscribe

my yearbook.component:

import {Component} from '@angular/core'
import {Olders} from './olders'
import {OldersService} from './olders.service'

@Component({
  moduleId: module.id,
  selector: 'yearbook',
  templateUrl: 'yearbook.component.html',

})

export class YearbookComponent {

  olders : Olders[];

  constructor(
    private _oldersService : OldersService
  ){}


  ngOnInit() {

    this._oldersService.getOldersfromAPI()
                       .subscribe( res => {this.olders = res,
                                           console.log(this.olders)},
                                   err => console.error(err.status)
                                 );


  }
 }

和olders.service:

import {Injectable} from '@angular/core'
import {Http} from '@angular/http'
import {Olders} from './olders'
import 'rxjs/add/operator/map'
import 'rxjs/add/operator/do'

@Injectable ()

export class OldersService {

  constructor(private _http:Http) {

  }

  getOldersfromAPI(){

    return this._http.get('../CDN/olders.json')
                     .do(x => console.log(x))
                     .map(olders => {olders = olders.json()});
  }
}

提前感谢所有答案的人

1 个答案:

答案 0 :(得分:2)

您在映射中缺少return语句:

.map(olders => {olders = olders.json()});

应该是:

.map(olders => { return olders = olders.json()});