1
votes

So I have two variables in my Typescript class:

private myServiceSubscription: Subscription;
myVar: myDto[] = [];

In the ctor:

this.myServiceSubscription = this.deliveryPointService
        .getPostalAddresses()
        .subscribe(result => console.log(result));

I can see the expected result returned from the service in the console log (a list with two items) However, if I use another subscribe instead:

.subscribe(result => this.myVar);

and (outside the subscribe): console.log(this.myVar[0]);

then I notice this.myVar is undefined.

Why does console.log(result) show the right result, but this.myVar does not?

This is in angular 4.

1

1 Answers

4
votes

Right now, you don't do anything with the result of your service method. You have to assign the returned item to your variable:

.subscribe(result => {
   this.myVar = result;
   console.log(this.myVar);
});