Arrays 访问Angular 5中的json字段并放入变量
使用ngFor将json放入html相对容易,但我需要在组件的ts中获取json字段的值,以便将其放入图形中 我的json中有一个名为“value”的字段。如何将所有值放入数组中 到目前为止,我的代码是:Arrays 访问Angular 5中的json字段并放入变量,arrays,json,angular,Arrays,Json,Angular,使用ngFor将json放入html相对容易,但我需要在组件的ts中获取json字段的值,以便将其放入图形中 我的json中有一个名为“value”的字段。如何将所有值放入数组中 到目前为止,我的代码是: import { WeatherstatsService } from '../weatherstats.service'; import 'rxjs/add/operator/map'; @Component({ selector: 'weatherchart', template
import { WeatherstatsService } from '../weatherstats.service';
import 'rxjs/add/operator/map';
@Component({
selector: 'weatherchart',
templateUrl: './weatherchart.component.html',
styleUrls: ['./weatherchart.component.css']
})
export class WeatherchartComponent implements OnInit {
constructor(private weatherstatsservice: WeatherstatsService) { }
title = "Title";
data: any;
datavalues = [];
chart = new Chart({
chart: {
type: 'line'
},
title: {
text: this.title,
},
credits: {
enabled: false
},
series: [{
name: 'Line 1',
data: [1, 2, 3]
}]
});
// add point to chart serie
add() {
this.chart.addPoint(Math.floor(Math.random() * 10));
}
getyeardata(data, datavalues) {
}
ngOnInit() {
this.weatherstatsservice.getWeatherData()
.subscribe(data => {
this.data = data;
})
console.log(value);
// console.log(this.data)
}
}
我的json看起来像这样,但非常庞大:
[{"id":1,"measurement_type":"Tmax","location":"Wales","year":1910,"month_or_season":"JAN","value":6.1},{"id":2,"measurement_type":"Tmax","location":"Wales","year":1910,"month_or_season":"FEB","value":7.2},{"id":3,"measurement_type":"Tmax","location":"Wales","year":1910,"month_or_season":"MAR","value":8.9},{"id":4,"measurement_type":"Tmax","location":"Wales","year":1910,"month_or_season":"APR","value":9.6},{"id":5,"measurement_type":"Tmax","location":"Wales","year":1910,"month_or_season":"MAY","value":14.5},{"id":6,"measurement_type":"Tmax","location":"Wales","year":1910,"month_or_season":"JUN","value":17.1},{"id":7,"measurement_type":"Tmax","location":"Wales","year":1910,"month_or_season":"JUL","value":17.3},{"id":8,"measurement_type":"Tmax","location":"Wales","year":1910,"month_or_season":"AUG","value":16.9},{"id":9,"measurement_type":"Tmax","location":"Wales","year":1910,"month_or_season":"SEP","value":15.6},{"id":10,"measurement_type":"Tmax","location":"Wales","year":1910,"month_or_season":"OCT","value":12.5},
json字段包括id、度量值类型、位置、年份、月份或季节
错误日志:
WeatherchartComponent.html:7 ERROR TypeError: Cannot read property 'year' of null
at Object.eval [as updateRenderer] (WeatherchartComponent.html:7)
at Object.debugUpdateRenderer [as updateRenderer] (core.js:14378)
at checkAndUpdateView (core.js:13514)
at callViewAction (core.js:13859)
at execEmbeddedViewsAction (core.js:13817)
at checkAndUpdateView (core.js:13510)
at callViewAction (core.js:13859)
at execComponentViewsAction (core.js:13791)
at checkAndUpdateView (core.js:13515)
at callViewAction (core.js:13859)
你试过这个吗
也张贴对象
更新:发布对象的标准形式,以便我能更好地理解
.subscribe(data => {
this.data = data;
this.data.forEach( result => {
this.datavalues.push(result.value);
});
})
尝试将请求和所有内容放在构造函数中,而不是放在ngOnInit中
你试过这个吗
也张贴对象
更新:发布对象的标准形式,以便我能更好地理解
.subscribe(data => {
this.data = data;
this.data.forEach( result => {
this.datavalues.push(result.value);
});
})
尝试将请求和所有内容放在构造函数中,而不是放在ngOnInit中
ngOnInit() {
this.weatherstatsservice.getWeatherData()
.subscribe(data => {
if(JSON.stringify(data)!=[]){
this.data=data;
this.data.forEach( res => {
this.datavalues.push(res.value);
});
}
})
}
(括号已更正)试试这个
ngOnInit() {
this.weatherstatsservice.getWeatherData()
.subscribe(data => {
if(JSON.stringify(data)!=[]){
this.data=data;
this.data.forEach( res => {
this.datavalues.push(res.value);
});
}
})
}
(括号已更正)我必须这样做。data.value,它不允许我?我得到错误类型错误:无法读取未定义的属性“push”将得到此错误?未捕获错误:syntaxError(compiler.js:466)中的模块“AppModule”在compiler.js:15121的Array.forEach()中声明了意外的值“undefined”…我必须这样做。data.value,它不允许我这样做?我得到错误类型错误:无法读取undefinedStill的属性“push”?我得到了这个错误吗?未捕获错误:syntaxError(compiler.js:466)中的模块“AppModule”在compiler.js:15121的Array.forEach()中声明了意外值“undefined”……我仍然收到此错误吗?compiler.js:466未捕获错误:模块“AppModule”声明的意外值“undefined”-我是否需要在App.module中导入其他内容?是否已将WeatherstatsService添加到App.module中的提供程序?是的,它就在那里!好的,我刚刚从命令行重新启动了angular服务器,它现在似乎正在工作!我仍然得到这个错误?compiler.js:466未捕获错误:模块“AppModule”声明的意外值“undefined”-我是否需要在App.module中导入其他内容?是否已将WeatherstatsService添加到App.module中的提供程序?是的,它就在那里!好的,我刚刚从命令行重新启动了angular服务器,它现在似乎正在工作!