The following does not output array to console. Scoping incorrect???
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Broker } from './broker';
@Injectable()
export class BrokerSurveyService {
brokers: Broker[] = [];
constructor(private http: HttpClient) {
this.getBrokers().subscribe((brokers) => {
this.brokers = brokers;
});
console.log(this.brokers); # Does NOT output to console
}
getBrokers() {
return this.http.get<Broker[]>('http://www.example.com/brokers.json');
}
}
This outputs the array to the console because it sent to console immediate after assignment. Is this scope different? I'm confused as to why.
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Broker } from './broker';
@Injectable()
export class BrokerSurveyService {
brokers: Broker[] = [];
constructor(private http: HttpClient) {
this.getBrokers().subscribe((brokers) => {
this.brokers = brokers;
console.log(this.brokers); # Outputs to console
});
}
getBrokers() {
return this.http.get<Broker[]>('http://www.example.com/brokers.json');
}
}