어쩌면 어딘가에서 어리석은 실수를 저지르고 있을지 모르지만 나는 더 이상 볼 수 없습니다.이오닉 2 각도 2 REST JSON 데이터 받기
나는 매우 간단한 이오니아 2 각도 2 응용 프로그램을 가지고 있습니다. 제발 아키텍처를 귀찮게하지 말고, 나는 단지 먼저 작동하도록 노력하고 있습니다. 배열 'configuredModules'에 REST JSON 서비스에서 가져온 Module 객체를 채우려고합니다. 콘솔에 배열을 기록하는 버튼을 만들었으므로 배열의 값은 무엇인지 알 수 있지만 항상 정의되지는 않습니다. 내가 도대체 뭘 잘못하고있는 겁니까?
export class Module {
type: string;
physicalLocationName: string;
}
내 메인 클래스 : 그래서
{"modules":[{"type":"myTestType","physicalLocationName":"myTestPhysicalLocation"}]}
: 나는 크롬의 내 네트워크 탭에 보면
import { Component, OnInit } from '@angular/core';
import { Module } from './module';
import { HttpModule, Http, Response} from '@angular/http';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/toPromise';
import { NavController } from 'ionic-angular';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage implements OnInit{
configuredModules: Module[];
http: Http;
constructor(public navCtrl: NavController,private _http: Http) {
this.http = _http;
}
getConfiguredModules(){
this.getModules()
.then(modules => this.configuredModules = modules);
console.log('finished configuredModules');
}
getModules(): Promise<Module[]> {
return this.http.get('http://localhost:8080/rest/modules/configuredModules')
.toPromise()
.then(response => response.json().data as Module[])
;
}
ngOnInit() {
this.getConfiguredModules();
}
buttonClicked(){
console.log('button clicked');
console.log(this.configuredModules.values);
}
}
, 나는 다음 (응답에서)에서 오는 것을 볼 수 있습니다 데이터가 들어오는 지 알지만 배열로 채워지지 않습니다. 어떤 제안? 나는 내 코드 각도 2의 영웅 튜토리얼을 향한 동일한 보이는 생각 : https://angular.io/docs/ts/latest/tutorial/toh-pt6.html
당신이 CONSOLE.LOG을 시도해 봤어 (JSON.stringify (this.configuredModules)); ? – stwissel
정확히 어디에서 정의되지 않습니까? 버튼 클릭 이벤트? 그것은'console.log (this.configuredModules); '이어야합니다. – micronyks
배열이 정의되지 않았습니까? 또한 .values를 witout으로 기록하려고 시도했지만 정의되지 않은 상태가되었습니다. 위 예제에서와 같이 .values를 사용하면 다음과 같이됩니다. TypeError : 정의되지 않은 'values'속성을 읽을 수 없습니다. 어느 날 내가 configuredModules가 정의되지 않았다고 믿게한다. JSON.stringify, 동일한 결과 : undefined –