简体   繁体   English

如何使用Angular2从WebAPI获取数据

[英]How to take data from webapi using angular2

TestTime.ts: TestTime.ts:

export class TestTime {
    id: number;
    taskRepositoryID: any;
    timesheetID: any;
    timeCategoryID: any;
    startTime: string;
    endTime: string;
    duration: any;
    comment: string;
}

testTime.service.ts: testTime.service.ts:

import { Injectable } from '@angular/core';
import { TestTime } from './TestTime';
import { HttpClient, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse } from '@angular/common/http';
import 'rxjs/Rx';
import { Observable } from 'rxjs/Observable';

 @Injectable()
export class TestTimeService {
public values: any;
constructor(public _http: HttpClient) { }
private _timeTestURL = 'http://localhost:3186/myData;

getTimes() {
    return this._http.get<TestTime>(this._timeTestURL);
}

public getAll<TestTime>(): Observable<TestTime> {
    return this._http.get<TestTime>(this._timeTestURL);
}
}

testTime.component.ts testTime.component.ts

import { Component, OnInit } from '@angular/core';
import { TestTime } from './TestTime';
import { TestTimeService } from './testTime.service';



@Component({
selector: 'app-testTime-component',
template: `
 <h1> Test Time </h1>
   <ul>
<li ngFor="let time of times">
 <span> {{time}} </span>
</li>
 </ul>
`
})


 export class TimeTestComponent {

constructor(private _timeServcie: TestTimeService) { }

errorMessage: string;
public times: TestTime[];
public TimeSheetData: string;
ngOnInit() {


    this._timeServcie
        .getAll<TestTime[]>()
        .subscribe((data: TestTime[]) => this.times = data,
        error => (ex) => {
            console.log("error" + ex);
        },
        () => {
            console.log("error error"); 
        });
}

 }

Above you can see my code. 您可以在上方看到我的代码。 I can't figure out why I am not getting the data. 我不知道为什么我没有得到数据。 After the " Test Time " which is in the template of timeTest.component.ts , there is nothing which means I am not even taken correctly the data, cuz if I was taking it correctly at least there should have been the . timeTest.component.ts模板中的“ 测试时间之后 ,没有任何东西表示我什至没有正确地获取数据,因为如果我正确地获取了数据,至少应该有 from the list items 从清单项目

why are you casting the http.get()methods, what you should do instead is to define the return type of the methods and then you will be able to see error more clearly. 为什么要转换http.get()方法,您应该做的是定义方法的返回类型,然后您将可以更清楚地看到错误。 try this 尝试这个

getTimes(): Observable<TestTime> {
    return this._http.get(this._timeTestURL);
}

public getAll(): Observable<TestTime[]> {
    return this._http.get(this._timeTestURL);
}

and then your observer in component should be like 然后您的组件观察者应该像

this._timeServcie.getAll().subscribe(
        (data: TestTime[]) => this.times = data,
        error => (ex) => {
            console.log("error" + ex);
        },
        () => {
            console.log("completed"); //THIS ISN'T ERROR
        });

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM