user7423539
user7423539

Reputation:

Interpolation not working in angular2

import {Component} from 'angular2/core';
import {Observable} from 'rxjs/Observable';
import {Http} from 'angular2/http';
import {SpsheetService} from './spsheet.service';
import 'rxjs/Rx';


@Component({
 selector: 'my-spsheet',
 template: `
             <button id="fetch" (click)="reloadData()">Reload Data</button>
            <table class="table">
            <thead>
                <tr>
                    <th *ngFor="#head of heads">
                        {{head | async}}
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr *ngFor="#dat of data">
                    <td *ngFor="#head of heads" contenteditable="true">
                        {{dat.head | async}}
                    </td>
                </tr>
            </tbody>
        </table>
        `,
providers: [SpsheetService]
 })

export class SpsheetComponent {
    data:any[];
    heads:string[];
    otherdata:string[];
    constructor(private _http:Http,private _spservice:SpsheetService){}

    reloadData(){
        this._spservice.fetchData()
            .subscribe(function(data) 
            {
                var me =this;
                me.data =[];
                me.data = data;
                me.heads = Object.keys(data[0]);
                var len = me.heads.length;
                console.log(me.heads);

            },
            err => console.log(err),
            () => console.log('Completed'));
    }


 }

I am loading data from a JSON file and structuring it into an HTML table. I am able to fetch the data and also structure it but the problem is it wont reflect as an HTML table, ie interpolation isn't working. Although the variables are carrying there respective values.

Upvotes: 0

Views: 1319

Answers (3)

Mukul Katal
Mukul Katal

Reputation: 161

use arrow functions instead to keep the context of 'this' intact.

reloadData() {
            this._spservice.fetchData()
                .subscribe((data) => {
                    this.data = [];
                    this.data = data;
                    this.heads = Object.keys(data[0]);
                    var len = this.heads.length;
                    console.log(this.heads);
                },
                err => console.log(err),
                () => console.log('Completed'));
        }

Upvotes: 4

user7423539
user7423539

Reputation:

I have worked around it, it was actually an issue with 'this' instance of the subscriber.

The actual correct code is:

import {Component} from 'angular2/core';
import {Observable} from 'rxjs/Observable';
import {Http} from 'angular2/http';
import {SpsheetService} from './spsheet.service';
import 'rxjs/Rx';


@Component({
 selector: 'my-spsheet',
 template: `
             <button id="fetch" (click)="reloadData()">Reload Data</button>
            <table class="table">
            <thead>
                <tr>
                    <th *ngFor="#head of heads">
                        {{head}}
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr *ngFor="#dat of data">
                    <td *ngFor="#head of heads" contenteditable="true">
                        {{dat[head]}}
                    </td>
                </tr>
            </tbody>
        </table>
        `,
providers: [SpsheetService]
 })

export class SpsheetComponent {
    data:any[];
    heads:string[];
    otherdata:string[];
    constructor(private _http:Http,private _spservice:SpsheetService){}

    reloadData(){
        var me =this;
        me._spservice.fetchData()
            .subscribe(function(data) 
            {
                me.data =[];
                me.data = data;
                me.heads = Object.keys(data[0]);
                var len = me.heads.length;
                console.log(me.heads);

            },
            err => console.log(err),
            () => console.log('Completed'));
    }


 }

Upvotes: 1

Shiv Kumar Ganesh
Shiv Kumar Ganesh

Reputation: 3825

You have got an Extra BackTick that is causing the entire code to be a string. Just remove that. Copy paste this code below:-

import {Component} from 'angular2/core';
import {Observable} from 'rxjs/Observable';
import {Http} from 'angular2/http';
import {SpsheetService} from './spsheet.service';
import 'rxjs/Rx';


@Component({
 selector: 'my-spsheet',
 template: `
             <button id="fetch" (click)="reloadData()">Reload Data</button>
            <table class="table">
            <thead>
                <tr>
                    <th *ngFor="#head of heads">
                        {{head | async}}
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr *ngFor="#dat of data">
                    <td *ngFor="#head of heads" contenteditable="true">
                        {{dat.head | async}}
                    </td>
                </tr>
            </tbody>
        </table>
        `,
],
providers: [SpsheetService]
 })

export class SpsheetComponent {
    data:any[];
    heads:string[];
    otherdata:string[];
    constructor(private _http:Http,private _spservice:SpsheetService){}

    reloadData(){
        this._spservice.fetchData()
            .subscribe(function(data) 
            {
                var me =this;
                me.data =[];
                me.data = data;
                me.heads = Object.keys(data[0]);
                var len = me.heads.length;
                console.log(me.heads);

            },
            err => console.log(err),
            () => console.log('Completed'));
    }


 }

Upvotes: 2

Related Questions