/ / Jak uzyskać odpowiedź usługi w tablicy do użycia, w angular 2 - angular, angular2-template, angular2-services

Jak uzyskać odpowiedź usługi w tablicy do użycia, w kątowej 2 - kątowej, kątowej2-szablon, angular2-services

Zadeklarowałem plik array.at .ts:

responsearray:any=[];

Konstruktor wewnętrzny:

constructor(private _dataService: UserService, private _ngZone: NgZone) {
this.responsearray = this.getmarkers(id);
this.moveMarker(this.responsearray);
}

Chcę przekazać zaktualizowaną wartość tablicy odpowiedzi w funkcji movemarker, a także zaktualizowano wewnątrz getmarkerów funkcji, ale zaktualizowane wartości nie zostały odzwierciedlone powyżej:

getmarkers(id) {

this._dataService
.GetMarker(id)
.subscribe(res => {
this.responseformate = Object.assign({}, res);
this.responsearray = this.responseformate.responsepacket;

},
error => console.log(error),
() => {
return (this.responsearray);
})
}

Odpowiedzi:

0 dla odpowiedzi № 1

Kwestia :

Zwracasz wartość z połączenia asynchronicznego, jeśli takchcesz to zrobić w ten sposób, to musisz zwrócić obserwowalne, a następnie subskrybować zdarzenie, po prostu zwracając wartość nie uzyskasz oczekiwanego rezultatu, zwróci obserwowalne.

Prostym sposobem na osiągnięcie wyniku jest:

constructor(private _dataService: UserService, private _ngZone: NgZone) {
this.getmarkers(id);
}

getmarkers(id) {
this._dataService
.GetMarker(id)
.subscribe(res => {
this.responseformate = Object.assign({}, res);
this.responsearray = this.responseformate.responsepacket;
this.moveMarker(this.responsearray);
},
error => console.log(error));
}

Inny sposób :

constructor(private _dataService: UserService, private _ngZone: NgZone) {
this.getmarkers(id).subscribe(() => {
this.moveMarker(this.responsearray);
});
}

getmarkers(id):Observable {
return this._dataService
.GetMarker(id)
.map(res => {
this.responseformate = Object.assign({}, res);
this.responsearray = this.responseformate.responsepacket;
},
error => console.log(error));
}