晚上好。角2未定義獲得外部訂閱
從我訂閱的訂閱服務中檢索數據時遇到問題;我在訂閱功能中獲取數據,但在其外部是UNDEFINED;
這是代碼。
userservice.ts
import { Injectable } from "@angular/core";
import { Http, Response, Headers } from "@angular/http";
import 'rxjs/Rx';
import { Observable } from "rxjs";
@Injectable()
export class UserService{
constructor(private http: Http){
}
getRegistrations(): Observable<any> {
return this.http.get('http://127.0.0.1:8000/api/candidatesL')
.map(
(response: Response) => {
return response.json().candidates;
}
);
}
}
全registration.ts
import { Component, OnInit } from '@angular/core';
import { NgForm } from "@angular/forms";
import { Candidate } from "../candidate.interface";
import { Response } from "@angular/http";
import { UserService } from "../user.service";
@Component({
selector: 'app-all-registration',
templateUrl: './all-registration.component.html',
styleUrls: ['./all-registration.component.css']
})
export class AllRegistrationComponent implements OnInit {
candidates: Candidate[];
constructor(private userService: UserService) {}
ngOnInit() {
this.getRegistration()
console.log(this.candidates);
}
getRegistration(){
this.userService.getRegistrations()
.subscribe(
(candidates: Candidate[]) => this.candidates = candidates,
(error: Response) => console.log(error),
)
}
}
當我的.subscribe(...)我可以顯示數據,但外面我裏面得到UNDEFINED。
請我等待着你的答案...
這個問題可能會問100次 – omeralper