我有一個服務,並在角2應用程序中,我得到的服務,並能記錄也控制檯響應,但它顯示的成分不確定的一個組成部分。我在這裏做什麼錯了?任何人都可以指出這個錯誤。 在此先感謝角2訂閱觀察到越來越不確定
服務:
import { Injectable } from '@angular/core'
import { Http, Response,Headers } from '@angular/http'
import { Observable } from 'rxjs/Observable'
import "rxjs/add/operator/map"
import "rxjs/add/operator/catch"
import "rxjs/add/Observable/throw"
@Injectable()
export class LoginService {
public logResult;
private _url = "http://localhost:53798/api/Login?uname=abcd&pass=1"
constructor(private _http: Http) { }
getEmployees(userID:string,passWord:string) : Observable<boolean>{
return this._http.get(this._url).map((response: Response) =>
{
let logResult= response.json();
logResult.forEach(re=>{
if(re.LoginResult=="Success")
{
console.log('Success');//this is being written to console
return<boolean> true;
}
else{
console.log('fail');
return<boolean> false;
}
})
}).catch(this._errorHandler);
}
_errorHandler(error: Response) {
console.log(error);
return Observable.throw(error || "Server not Found")
}
}
及部件:
ValidateLogin(){
this._loginService.getEmployees(this.loginForm.value.UserId,this.loginForm.value.password)
.subscribe(resLoginData => {
console.log(resLoginData);//resLoginData is undefined
if(resLoginData===true)
{
console.log('Success');
this.router.navigateByUrl('/Home');}
else{
console.log('Fail');
this.router.navigateByUrl('/Login');
}
resLoginError => this.errorMsg = resLoginError})
}
}
我認爲你沒有在'map'函數中正確返回響應,請嘗試像這樣'return logResult.forEach(re => {....' –