从角度服务获取数据并控制台记录

时间:2019-02-07 04:33:28

标签: angular typescript

我正在尝试将app.component文件中的console.log数据添加到服务文件中。我变得不确定。

这是我的代码

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

}

1 个答案:

答案 0 :(得分:5)

您需要将控制台日志放置在订阅中,

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