How to get response of a service in an array for use, in angular 2

mehul

I had declare an array.at .ts file:

responsearray:any=[];

Inside constructor:

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

I want to pass updated value of response array in movemarker function and it also updated inside function getmarkers, but updated values not reflected above:

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);
        })
 }
Vivek Doshi

Issue :

You are returning value from async call , if you want to do it that way then you have to return observables and then have to subscribe the event , by simply returning value you will not get the expected result it will return the observable.

Simple way to achieve output is :

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));
}

Another way :

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));
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get JSON array response data in angular2 application

From Dev

How to get the elements of an array returned by a service in Angular 2?

From Dev

How to get Angular 2 response headers

From Dev

How to get whole array from service Angular?

From Dev

How to use service in angular2 NgModule

From Dev

how to get data from service in angular 2?

From Dev

how to use a service in angular

From Dev

how to display array of http response in angular2 cli?

From Dev

How to get value in Response header Angular2

From Dev

How can get data complicated json response in angular 2?

From Dev

How to get Header Response in angular 2 without external link?

From Dev

Angular2 how to process service which is not array

From Dev

How to use Promise in angular2 with firebase to transform login to service

From Dev

How to use Promise in angular2 with firebase to transform login to service

From Dev

How to use dependency injection in service based Angular2 project

From Dev

How can I get the response of an interceptor on my own defined service in Angular.js?

From Dev

Angular 2: How to pass service config before it get created?

From Dev

How to use jQuery to call asp.net asmx web service with parameters to get response

From Dev

How to get the JSON of a response from a service

From Dev

Angular: How to use a service in a for loop?

From Dev

could not update a variable in an angular service with ajax response data and use is to filter

From Dev

file download is damaged when use observe: response in angular service

From Dev

could not update a variable in an angular service with ajax response data and use is to filter

From Dev

(Angular JS) How filter $http.get by passing params if the response is an array of JSON objects?

From Dev

How to get HTTP JSON-response into HTML (Ionic2, Angular2, TypeScript, PHP, JSON)?

From Dev

Angular2 handling response from service in component

From Dev

Angular2: Loading components dynamically from a service response

From Dev

Angular2 choose template dynamically according to service response

From Dev

Angular2: Service with http - handling errors (in 200 response)

Related Related

  1. 1

    How to get JSON array response data in angular2 application

  2. 2

    How to get the elements of an array returned by a service in Angular 2?

  3. 3

    How to get Angular 2 response headers

  4. 4

    How to get whole array from service Angular?

  5. 5

    How to use service in angular2 NgModule

  6. 6

    how to get data from service in angular 2?

  7. 7

    how to use a service in angular

  8. 8

    how to display array of http response in angular2 cli?

  9. 9

    How to get value in Response header Angular2

  10. 10

    How can get data complicated json response in angular 2?

  11. 11

    How to get Header Response in angular 2 without external link?

  12. 12

    Angular2 how to process service which is not array

  13. 13

    How to use Promise in angular2 with firebase to transform login to service

  14. 14

    How to use Promise in angular2 with firebase to transform login to service

  15. 15

    How to use dependency injection in service based Angular2 project

  16. 16

    How can I get the response of an interceptor on my own defined service in Angular.js?

  17. 17

    Angular 2: How to pass service config before it get created?

  18. 18

    How to use jQuery to call asp.net asmx web service with parameters to get response

  19. 19

    How to get the JSON of a response from a service

  20. 20

    Angular: How to use a service in a for loop?

  21. 21

    could not update a variable in an angular service with ajax response data and use is to filter

  22. 22

    file download is damaged when use observe: response in angular service

  23. 23

    could not update a variable in an angular service with ajax response data and use is to filter

  24. 24

    (Angular JS) How filter $http.get by passing params if the response is an array of JSON objects?

  25. 25

    How to get HTTP JSON-response into HTML (Ionic2, Angular2, TypeScript, PHP, JSON)?

  26. 26

    Angular2 handling response from service in component

  27. 27

    Angular2: Loading components dynamically from a service response

  28. 28

    Angular2 choose template dynamically according to service response

  29. 29

    Angular2: Service with http - handling errors (in 200 response)

HotTag

Archive