gpt4 book ai didi

javascript - 在 http.get() 之后将结果保存在 Angular 4 的数组中

转载 作者:行者123 更新时间:2023-11-30 15:12:16 25 4
gpt4 key购买 nike

我使用 http.get() 从一个 api 中检索一个对象数组。每个项目内部只有一个属性:“数字”。我希望将该属性存储在我的 app.component 的数组中。然后我将使用该数组作为使用 chart.js 的图表的数据,目前工作正常。我已经尝试了一天多,但不知道该怎么做。你能告诉我吗?

data that I get from api

应用程序组件.ts

import { Component } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/Rx';

import * as moment from 'moment';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app';

nrs: any;
count: any;
public listNrs: Array<number> = [];

private apiAllUrl: string = 'http://localhost:3000/api/all';
private apiCountUrl: string = 'http://localhost:3000/api/count';
//private apiBothUrl: string = 'http://localhost:3000/api/both';

constructor(private http:Http) {
let now = moment.locale("it");
}

ngOnInit() {
this.getAllNrs();
this.getCountNrs();
this.getDataNumbers();
//this.getCount();
//this.loadData();
}

getAllNrs() {
this.http.get(this.apiAllUrl)
.map((res:Response) => res.json())
.subscribe(
data => { this.nrs = data},
err => console.error(err),
() => console.log(this.nrs)
);
}

getCountNrs(){
this.http.get(this.apiCountUrl)
.map((res:Response) => res.json())
.subscribe(
data => { this.count = data},
err => console.error(err),
() => console.log(this.count)
);
}

getDataNumbers(){

}

//tried using jsonp
/*loadData() {
this.jsonp.get(this.apiAllUrl)
.map(res => res.json())
.subscribe(data => console.log(data));
}*/

/*getCount(){
this.getAllNrs();
for(let nr of this.nrs){
console.log(this.nrs.nr[1]);
}
}*/

newDate(days) {
return moment().add(days, 'days');
}

public lineChartData:Array<any> = [
{data: [4, 5, 56, -23, 4,], label: 'Indexer meta served links'},
];

public lineChartData1:Array<any> = [
{data: [-23, 4, 4, 5, 56], label: 'Indexer served links'},
];

public lineChartData2:Array<any> = [
{data: [4, 5, 4, 56, -23], label: 'Real-Time served links'},
];

public lineChartLabels:Array<any> = ['1 Lug 2017', '2 Lug 2017', '3 Lug 2017', '4 Lug 2017', '5 Lug 2017'];

public lineChartOptions:any = {
responsive: true,
/*
scales: {
xAxes: [{
type: 'time',
time: {
displayFormats: {
'millisecond': 'DD MMM',
'second': 'DD MMM',
'minute': 'DD MMM',
'hour': 'DD MMM',
'day': 'DD MMM',
'week': 'DD MMM',
'month': 'DD MMM',
'quarter': 'DD MMM',
'year': 'DD MMM',
}
}
}],
},
*/
};

public lineChartColors:Array<any> = [
{ // grey
backgroundColor: 'rgba(255,55,55,0.2)',
borderColor: 'rgba(255,55,55,1)',
pointBackgroundColor: 'rgba(255,55,55,1)',
pointBorderColor: '#fff',
pointHoverBackgroundColor: '#fff',
pointHoverBorderColor: 'rgba(255,55,55,0.8)'
},
];

public lineChartLegend:boolean = true;
public lineChartType:string = 'line';

// events
public chartClicked(e:any):void {
console.log(e);
}

public chartHovered(e:any):void {
console.log(e);
}
}

应用程序模块.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';
import { ChartsModule } from 'ng2-charts/ng2-charts';

/*import { DataService } from './data.service';
import { DataComponent } from './data/data.component';*/

@NgModule({
declarations: [
AppComponent,
/*DataComponent*/,
],
imports: [
BrowserModule,
HttpModule,
ChartsModule
],
providers: [/*DataService*/],
bootstrap: [AppComponent]
})
export class AppModule { }

getAllNrs() 获取一个对象数组,其中每个对象都有一个名为 number 的属性。

getAllCount() 获取一个对象数组,其中每个对象都有一个名为 count 的属性。

问题是,即使我尝试将它映射到数组中,它也会告诉我 this.nrs 未定义且无法获取其长度(错误:无法读取属性“长度”未定义的)。你知道它为什么这样做吗?

如果您还需要什么,请告诉我,我会提供。谢谢!

最佳答案

根据您的评论,您需要在构造函数中初始化所有属性,否则它将得到 undefined。即使你给它一个类型,Typescript 也不能​​神奇地知道它的默认值。

export class AppComponent {
title = 'app';
nrs: any;
count: any;
public listNrs: Array<number> = [];
constructor(private http: Http) {
let now = moment.locale("it");
//initialize your values.
this.nrs = [];
this.count = 0;
}
//other codes
}

之后,您可以使用 .map() 在您的 getAllNrs() 中返回一个数字数组

getAllNrs() {
this.http.get(this.apiAllUrl)
.map((res: Response) => res.json())
.subscribe(
data => {
this.nrs = data.map(nrObj => nrObj.number)
},
);
}

关于javascript - 在 http.get() 之后将结果保存在 Angular 4 的数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44943612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com