我的service.ts没有将数据返回到component.ts

时间:2016-08-26 20:50:45

标签: angular

我的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);
    }
}

我的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();
    }
}

我的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>

我的Web API服务按预期工作 - 它返回 enter image description here

Web服务已经过AngularJS测试 - 按预期工作。

所以,我的问题是:我从JSON格式的Web API服务获取数据。我可以在调试中看到它们。出于某种原因,我的&#34; ftp-requests.component.ts&#34;没有从ftp-request.service.ts接收数据。

是的,我在app.module.js中导入了HttpModule,JsonpModule。 我的网络面板看起来很好: enter image description here

有人能发现问题吗?

2 个答案:

答案 0 :(得分:0)

问题是您在提取数据之前尝试显示getFTPRequests()数据。简单的*ngIf指令将解决您的所有问题:

<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>

答案 1 :(得分:0)

问题在于:

getFTPRequests(): Promise<FTPRequest[]> {
    return this.http.get(this.APIFTPRequestsURL)
            .toPromise()
            .then(response => response.json().data as FTPRequest[])
            .catch(this.handleError);
}

当我检查承诺回调时 - &#34; EXCEPTION:找不到不同的支持对象&#39; [object Object]&#39;类型&#39;对象&#39;。 NgFor仅支持绑定到诸如Arrays之类的Iterables。 &#34; 我已将getFTPRequests()更改为

getFTPRequests(): Promise<FTPRequest[]> {
    return this.http.get(this.APIFTPRequestsURL)
            .toPromise()
            .then(response => response.json() as FTPRequest[])
            .catch(this.handleError);
}

感谢JB Nizet和Stefan Svrkota的答案!