Chuck Tung Chuck Tung -3 years ago 121
TypeScript Question

angular class method not working when populated by service

I'm learning Angular 4 and I can't figure out how to get class methods to work.

I have a class for the feature, that includes some methods that allow constructing a google charts JSON datatable.

export class Feature {
id: string;
name: string;

get cols() {
return [
{'id': '', 'label': 'id', 'type': 'string'},
{'id': '', 'label': 'name', 'type': 'string'}

get row() {
return {'c': [ {'v':}, {'v':} ] };


I have a service that populates the features from a REST API:

import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';

import 'rxjs/add/operator/toPromise';
import { Feature } from './feature';

export class FeatureService {
private featureUrl = 'http://raspi/rest/api';

constructor(private http: Http) { }

getFeatures(): Promise<Feature[]> {
return this.http.get(this.featureUrl + '/getFeatures')
.then(response => response.json().results as Feature[])

private handleError(error: any): Promise<any> {
console.error('An error occurred', error); // for demo purposes only
return Promise.reject(error.message || error);

Then in my component, on init, I call the service, and attempt to convert the instance array into a Google datatable:

import { Component, OnInit } from '@angular/core';
import { Feature } from '../feature';
import { FeatureService } from '../feature.service';

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'},

private featureService: FeatureService
) { }

private features2dataTable(features: Feature[]) {
const data = {
'cols': [],
'rows': []

features.forEach(function(feature) {
data.cols = feature.cols;
data.rows.push( feature.row );
return data;

getFeatures(): void {
.then(features => this.tableData.dataTable = this.features2dataTable(features))

ngOnInit(): void {


I want the datatable to have this format:

"cols": [
"rows": [

but when I use feature.row or feature.cols, I get back undefined, even though the "id" and "name" fields of the instance are populated:

{id: "BP01", name: "BP feature one"}
id: "BP01"
name: "BP feature one"
__proto__: Object

{id: "BP02", name: "BP feature two"}
id: "BP02"
name: "BP feature two"
__proto__: Object

{cols: undefined, rows: Array(2)}
cols: undefined
rows: (2) [undefined, undefined]
__proto__: Object

Why won't the get method work?

Answer Source


.then(response => response.json().results as Feature[])

does not actually create an array of Feature objects, therefore you do not have access to any class specific methods, since they are just POJO's.

You need to explicitly make the objects instances of your class, so something like this:

getFeatures() {
  return this.http.get(this.featureUrl + '/getFeatures')
    .then(response => response.json() => Object.assign(new Feature(), obj)))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download