-1
HTTP 요청을 만들고 데이터를 반환하는 Angular 4 서비스가 있습니다. 그러나 콘솔에 결과를 기록 할 때 undefined
이 표시됩니다.HTTP 요청을하는 각도 서비스
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class MenuService {
constructor(private http: HttpClient) {
}
menu: any;
getMenu(){
this.http.get('/assets/MENU.json').subscribe(data => {
// Read the result field from the JSON response.
let newValue = JSON.stringify(data).replace('{"Node":', '[');
newValue = newValue.substring(0,newValue.length - 1);
newValue+="]";
this.menu=JSON.parse(newValue);
});
console.log(this.menu);
return this.menu;
}
}
먼저, 당신이 당신의 응답 실제의 내용물을 확인하기 위해 요청 함수 내에서 데이터 자체를 CONSOLE.LOG하려고합니다. 요청 내부 –
@ZiyaddinSadigov 나는 그들이 *로 * 콜백 내부에 응답을해야합니까 만 더 혼동 할 JSON 객체 – eli
@ZiyaddinSadigov를 얻을. 요점은 ** 비동기 **입니다. – jonrsharpe