我正在學習Angular 4,我無法弄清楚如何讓類方法工作。角度類方法不工作時填充服務
我有一個功能類,它包括一些允許構建谷歌圖表JSON數據表的方法。
export class Feature {
id: string;
name: string;
get cols() {
return [
{'id': '', 'label': 'id', 'type': 'string'},
{'id': '', 'label': 'name', 'type': 'string'}
];
}
get row() {
console.log('foo');
return {'c': [ {'v': this.id}, {'v': this.name} ] };
}
}
我有填充從REST API的功能服務:
import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import { Feature } from './feature';
@Injectable()
export class FeatureService {
private featureUrl = 'http://raspi/rest/api';
constructor(private http: Http) { }
getFeatures(): Promise<Feature[]> {
return this.http.get(this.featureUrl + '/getFeatures')
.toPromise()
.then(response => response.json().results as Feature[])
.catch(this.handleError);
}
private handleError(error: any): Promise<any> {
console.error('An error occurred', error); // for demo purposes only
return Promise.reject(error.message || error);
}
}
然後在我的組件,在初始化,我調用服務,並嘗試實例數組轉換成谷歌數據表:
import { Component, OnInit } from '@angular/core';
import { Feature } from '../feature';
import { FeatureService } from '../feature.service';
@Component({
selector: 'app-features',
templateUrl: './features.component.html',
styleUrls: ['./features.component.css']
})
export class FeaturesComponent implements OnInit {
features: Feature[];
public tableData = {
chartType: 'Table',
dataTable: {},
options: {'title': 'Tasks'},
};
constructor(
private featureService: FeatureService
) { }
private features2dataTable(features: Feature[]) {
const data = {
'cols': [],
'rows': []
}
features.forEach(function(feature) {
console.log(feature);
data.cols = feature.cols;
data.rows.push(feature.row);
});
console.log(data);
return data;
}
getFeatures(): void {
this.featureService
.getFeatures()
.then(features => this.tableData.dataTable = this.features2dataTable(features))
}
ngOnInit(): void {
this.getFeatures();
}
}
我想要的數據表中有這樣的格式:
{
"cols": [
{"id":"","label":"Topping","pattern":"","type":"string"},
{"id":"","label":"Slices","pattern":"","type":"number"}
],
"rows": [
{"c":[{"v":"Mushrooms","f":null},{"v":3,"f":null}]},
{"c":[{"v":"Onions","f":null},{"v":1,"f":null}]},
{"c":[{"v":"Olives","f":null},{"v":1,"f":null}]},
{"c":[{"v":"Zucchini","f":null},{"v":1,"f":null}]},
{"c":[{"v":"Pepperoni","f":null},{"v":2,"f":null}]}
]
}
但是當我使用feature.row或feature.cols,我回來不確定,即使「id」和實例的「名稱」字段填入:
{id: "BP01", name: "BP feature one"}
id: "BP01"
name: "BP feature one"
__proto__: Object
features.component.ts:30
{id: "BP02", name: "BP feature two"}
id: "BP02"
name: "BP feature two"
__proto__: Object
features.component.ts:34
{cols: undefined, rows: Array(2)}
cols: undefined
rows: (2) [undefined, undefined]
__proto__: Object
爲什麼不弄方法工作?
爲什麼你在同一行這樣分開吧'this.tableData.dataTable = this.features2dataTable(功能) '。你能否展示從數據庫中訪問時功能的外觀 –
難道你得到的響應不符合你期望的類定義嗎?請問console.log(response.json())的結果是什麼? –
對不起,「id」和「name」字段被正確填充。當我在我的features.forEach循環中執行console.log(feature)時,這是可見的。問題是每個功能只有「id」和「name」字段,並且沒有在類中定義的「row」或「cols」方法。 AJT_82確定了問題。我正在循環POJO而不是Feature對象。 –