My ftp-request.service.ts
import {Injectable} from '@angular/core';
import {FTPRequest} from './ftp-request';
import {Headers, Http, Response} from '@angular/http';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class FTPRequestService {
private APIFTPRequestsURL = '../api/APIFTPRequests';
constructor(private http: Http) {
}
getFTPRequests(): Promise<FTPRequest[]> {
return this.http.get(this.APIFTPRequestsURL)
.toPromise()
.then(response => response.json().data as FTPRequest[])
.catch(this.handleError);
}
private handleError(error: any): Promise<any> {
console.error('An error occurred', error);
return Promise.reject(error.message || error);
}
}
My ftp-requests.component.ts
import { Component, Input, OnInit } from '@angular/core';
import {Pipe} from '@angular/core';
import { FTPRequest } from './ftp-request';
import { FTPRequestService} from './ftp-requests.service';
@Component({
selector: 'ftp-requests',
templateUrl: 'app/FTPRequests/ftp-requests.component.html',
providers: [FTPRequestService]
})
export class FTPRequestsComponent implements OnInit {
ftprequests: FTPRequest[];
constructor(private ftpRequestService: FTPRequestService) { }
getFTPRequests(): void {
this.ftpRequestService.getFTPRequests().then(ftprequests => this.ftprequests = ftprequests);
}
ngOnInit(): void {
this.getFTPRequests();
}
}
my ftp-request.ts
export class FTPRequest {
FTPRequestId: number;
CreatorId: number;
FTPRequestDate: string;
FTPURL: string;
FTPRequestComments: string;
NickName: string;
}
ftp-requests.component.html
<div class="container-fluid">
<h1>FTP Requests</h1>
<div class="row">
<table class="table">
<tr *ngFor="let ftp of ftprequests">
<td>{{ftp.NickName| capitalize}}</td>
<td>{{ftp.FTPRequestId}}</td>
<td>{{ftp.FTPRequestDate}}</td>
<td>{{ftp.FTPURL}}</td>
<td>{{ftp.FTPRequestComments}}</td>
</tr>
</table>
</div>
</div>
My Web API service works as expected - it returns
And the web service has been tested with AngularJS - works as expected.
So, my question is: I am getting data from Web API service in JSON format. I can see them in my debug. For some reason my "ftp-requests.component.ts" is not receiving data from ftp-request.service.ts.
Yes, I am importing HttpModule, JsonpModule in my app.module.js. And my network panel looks OK:
Can somebody spot the problem?
Problem is you are trying to display getFTPRequests()
data before it is fetched. Simple *ngIf
directive will solve all your problems:
<div class="container-fluid">
<h1>FTP Requests</h1>
<div class="row" *ngIf="ftprequests">
<table class="table">
<tr *ngFor="let ftp of ftprequests">
<td>{{ftp.NickName| capitalize}}</td>
<td>{{ftp.FTPRequestId}}</td>
<td>{{ftp.FTPRequestDate}}</td>
<td>{{ftp.FTPURL}}</td>
<td>{{ftp.FTPRequestComments}}</td>
</tr>
</table>
</div>
The problem was in:
getFTPRequests(): Promise<FTPRequest[]> {
return this.http.get(this.APIFTPRequestsURL)
.toPromise()
.then(response => response.json().data as FTPRequest[])
.catch(this.handleError);
}
When I checked the promise callback - "EXCEPTION: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays. " I have changed getFTPRequests() to
getFTPRequests(): Promise<FTPRequest[]> {
return this.http.get(this.APIFTPRequestsURL)
.toPromise()
.then(response => response.json() as FTPRequest[])
.catch(this.handleError);
}
Thank you JB Nizet and Stefan Svrkota for your answers!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.