0
Ich bin neu in Typoskript und Angular 2.Typoskript: kann nicht auf Klasseneigenschaft
Ich mag würde die api Daten in getApiData
Methode zur Klasse Eigenschaft currentViewData
passieren.
aber ich bin immer diesen Fehler:
ERROR Error: Uncaught (in promise): TypeError: Cannot set property 'currentViewData' of undefined TypeError: Cannot set property 'currentViewData' of undefined
Das ist mein API-Servicecode:
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class ResourceService {
private baseUrl: string = 'http://api.com/blabla';
private currentViewData: object;
constructor(private http: Http) {
}
prepareSubPath(subPath: string): string {
if (subPath.substr(0, 1) !== '/') {
return '/' + subPath;
}
}
getApiData(subPath: string = ''): void {
let url = this.baseUrl + this.prepareSubPath(subPath);
console.log(url);
let data = this.http.get(url)
.toPromise();
data.then(function (resp) {
this.currentViewData = resp.json();
console.info(this.currentViewData);
})
}
}
Das verursachte den Fehler, danke! – btx