Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get data from angular service and console log it

I am trying to console.log data in my app.component file coming my service file. I am getting undefined.

Here is my code

service.ts

getBillingCycles() {
    return this.http.get('../../assets/download_1.json');
};

app.component.ts

export class AppComponent implements OnInit{
    title = 'ngMTN';
    billing: any;

    constructor(private http: HttpClient, private BillingCyclesService: BillingCyclesServiceService) { }

    ngOnInit() {
        this.getBillingCycles();
    }

    getBillingCycles() {
        this.BillingCyclesService.getBillingCycles()
        .subscribe(data => this.billing = data);
        console.log(this.billing);
    }

}
like image 992
RRB Avatar asked Dec 30 '25 04:12

RRB


1 Answers

You need to place the console log within the subscribe,

this.BillingCyclesService.getBillingCycles()
        .subscribe((data) => {
         this.billing = data;
         console.log(this.billing);
});
like image 148
Sajeetharan Avatar answered Dec 31 '25 18:12

Sajeetharan