1

I am having problems displaying information from an database to the angular 2 *ngFor loop. This is my code so far:

.Net Core controller:

     // GET: api/Hats
    [HttpGet("GetHats")]
    public IEnumerable<Hat> GetHats()
    {
        return _context.Hat;
    }

hat-list.component.ts:

    //Imports from @Angular
import { Component, OnInit } from '@angular/core';
import { Http } from '@angular/http';

//Other imports
import { HatListService } from '../service/service.component';
import Hat = App.Models.IHat;
//Component Config
@Component({
    selector: 'hat-list',
    templateUrl: './hat-list.component.html',
     providers: [HatListService]
})
//Class
export class HatListComponent implements OnInit {

    public Hats: any[];
    constructor(public _hatListService: HatListService) {

    }


    //OnInit
    ngOnInit() {
        this.getAllHats();
    }

    //Get All 
    getAllHats() {
        //debugger
        this._hatListService.getHatList().subscribe(foundHats =>
            this.Hats = foundHats
        );
    }
}

service.component.ts

//imports from Angular
import { Injectable, Component } from '@angular/core';
import { Http, Request, RequestMethod, Response, RequestOptions, Headers } from '@angular/http';
import 'rxjs/Rx';
import { Observable } from 'rxjs/Observable';
//Other imports
import Hat = App.Models.IHat;
@Component({
    providers: [Http]
})
//Exported class
@Injectable()
export class HatListService {

    public headers: Headers;

    constructor(private _http: Http) {
    }

    public _getUrl: string = '/api/Hats/GetHats';

    //Get
    getHatList(): Observable<Hat[]> {
        //debugger
        return this._http.get(this._getUrl).map(res => <Hat[]>res.json())
            .catch(this.handleError);
    }


    private handleError(error: Response) {
        return Observable.throw(error.json().error || 'Opps!! Server error');
    }
}

hat-list.component.html:

<table class="table table-hover table-striped table-responsive">
    <thead>
        <tr>
            <th>Name</th>
            <th>Color</th>
            <th>Count</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let hat of Hats"">

            <td>{{hat.Name}}</td>
            <td>{{hat.Color}}</td>
            <td>{{hat.Count}}</td>
            <td>
                <a class="glyphicon glyphicon-remove" (click)="delete(hat)"></a>
            </td>
        </tr>
    </tbody>
</table>

Picture of the table enter image description here

The *ngFor recives the value.

enter image description here

I know the request to the database happens asynch and that one problem might be that the *ngfor loop fires before the data has returned and thus no information is displayed. But diffrent articles on the net say that the *ngFor loop shouldt fire unless the iterable has a value. The strange thing is that when I update the database manually thru SSMS the *ngfor recognises the added content add creates additioanl rows. What am I doing wrong.

Thanx for all the help!

4
  • 1
    Can you show your Hat class? Not sure for 100% but I guess the issue is that you use incorrect names of your variables i.e. {{hat.Name}} instead of {{hat.name}}. Commented May 4, 2017 at 9:29
  • Soory for the awful formatting. Tried to format as code` declare module App.Models { interface IHat { Id: number; Name: string; Color: string; Count: number; } }` Commented May 4, 2017 at 9:30
  • You have my biggest thanx sir!!. Such a noobie mistake. changed the interface properties to small staring letters. The it worked. Commented May 4, 2017 at 9:40
  • add the letters in the html file ofcourse Commented May 4, 2017 at 9:43

1 Answer 1

1

There are wrong variable names used in the template i.e. {{hat.Name}} instead of {{hat.name}}.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.