简体   繁体   English

我的service.ts不会将数据返回到component.ts

[英]My service.ts doesn't return data to component.ts

My ftp-request.service.ts 我的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 我的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 我的ftp-request.ts

export class FTPRequest {
    FTPRequestId: number;
    CreatorId: number;
    FTPRequestDate: string;
    FTPURL: string;
    FTPRequestComments: string;
    NickName: string;
}

ftp-requests.component.html 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 我的Web API服务按预期工作 - 它返回 在此输入图像描述

And the web service has been tested with AngularJS - works as expected. 并且已经使用AngularJS测试了Web服务 - 按预期工作。

So, my question is: I am getting data from Web API service in JSON format. 所以,我的问题是:我从JSON格式的Web API服务获取数据。 I can see them in my debug. 我可以在调试中看到它们。 For some reason my "ftp-requests.component.ts" is not receiving data from ftp-request.service.ts. 由于某种原因,我的“ftp-requests.component.ts”没有从ftp-request.service.ts接收数据。

Yes, I am importing HttpModule, JsonpModule in my app.module.js. 是的,我在我的app.module.js中导入了HttpModule,JsonpModule。 And my network panel looks OK: 我的网络面板看起来很好: 在此输入图像描述

Can somebody spot the problem? 有人能发现问题吗?

Problem is you are trying to display getFTPRequests() data before it is fetched. 问题是你试图在获取之前显示getFTPRequests()数据。 Simple *ngIf directive will solve all your problems: 简单的*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>

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 当我检查promise回调时 - “EXCEPTION:找不到'对象'类型的不同支持对象'[object Object]'。NgFor只支持绑定到诸如Arrays之类的Iterables。”我已将getFTPRequests()更改为

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! 感谢JB Nizet和Stefan Svrkota的答案!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 service.ts 会在 component.ts 之前执行吗? - Does service.ts get executed before component.ts? component.ts 中的变量如何自动更新,而无需在 component.ts 中再次使用 updateData()/ngOnit; 何时调用 service.ts? - How variables in component.ts is automatically updating without using an updateData()/ngOnit again in component.ts ; when a service.ts is called? 如何从 service.ts 文件中的 Component.ts 文件调用 function,该文件在 angular 的同一组件中 - How to Call function from Component.ts file in service.ts file which is in the same component in angular 将数据从service.ts传输到组件 - Transferring data from service.ts to component 如何绑定选择/选项值以在service.ts中而不是component.ts中获得请求功能(角度8) - How to bind selected/option value to get request function in service.ts and not component.ts (Angular 8) 如何在发射成功时处理从 service.ts 到 component.ts 的 socket.io 确认 Angular 8 - How to handle socket.io acknowledgement from service.ts to component.ts on emit success Angular 8 如何将 component.ts 中的变量发送到 service.ts 进行身份验证 Angular - How to send variable in component.ts to service.ts for authentication Angular 订阅值在component.ts中返回undefined但在service.ts中存在值 - subscribed value returns undefined in component.ts but value exists in service.ts 与服务,component.ts和component.html的通信不起作用 - Communication with the service, the component.ts and the component.html doesn't work 我的 component.html 文件没有读取我的 component.ts 变量,有什么解决办法吗? - My component.html file doesn't read my component.ts variables, any solution?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM