简体   繁体   English

如何从Rest API获取数据到我的视图-Angular 6

[英]How to get data from a rest api into my view - Angular 6

I have a Angular 6 application, I would like to connect some rest api data into my application. 我有一个Angular 6应用程序,我想将一些其他api数据连接到我的应用程序中。 I have written a service for this. 我已经为此写了一份服务。 The rest api is: https://demo1049220.mockable.io/api/incident . 其余api是: https : //demo1049220.mockable.io/api/incident Which takes in data from a results object. 从结果对象中获取数据。 So far I have the following code but it does not work as the data is not showing in the table: 到目前为止,我有以下代码,但由于数据未在表中显示,因此无法正常工作:

Also error in console is: Uncaught TypeError: Cannot read property 'ngOriginalError' of undefined 控制台中的错误还包括:未捕获的TypeError:无法读取未定义的属性'ngOriginalError'

Service.ts 服务

import { Injectable } from '@angular/core';
import { HttpClient, HttpEventType, HttpHeaders, HttpRequest, HttpResponse, HttpErrorResponse } from '@angular/common/http';
import { Observable, of, throwError } from 'rxjs';
import { catchError, retry } from 'rxjs/operators';


@Injectable({
  providedIn: 'root'
})
export class nowService {

  serviceApiUrl: string = 'https://demo1049220.mockable.io/api/incident';

  constructor(
    private http: HttpClient,

  ) { }

  getAll(): Observable<any> {
    return this.http.get<any>(this.serviceApiUrl)
      .pipe(
        catchError(this.handleError)
      );
  }

  private handleError(error: HttpErrorResponse) {
    if (error.error instanceof ErrorEvent) {
      console.log(error.error.message)

    } else {
      console.log(error.status)
    }
    return throwError(
      console.log('Something has happened; Api is not working!!'));
  };

}

Component.ts Component.ts

import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpEventType, HttpHeaders, HttpRequest, HttpResponse } from '@angular/common/http';
// Services 
import { nowService } from '../../services/servicenow.service';


@Component({
  selector: 'app-service-incident',
  templateUrl: './service-incident.component.html',
  styleUrls: ['./service-incident.component.scss']
})

export class ServiceIncidentComponent implements OnInit {

  public incidents: any; 
  public loading = true;
  public errorApi = false;


  constructor(private service: nowService) {

  }

  ngOnInit() {
    this.service.getAll().subscribe((data) => {
      this.loading = true;
      this.incidents = data;
      this.loading = false;
      console.log('Result - ', data);
      console.log('data is received');
    })
  }
}

Table in html to list the data HTML中的表格以列出数据

<tbody>
          <tr class="" *ngFor="let incident of incidents">
            <td><input type="radio" name="gender">
              <i class="form-icon mr-5"></i>{{incident.u_serial_number}}</td>
            <td>{{incident.u_product_name}}</td>
            <td>{{incident.u_address_floor}}</td>
            <td>{{incident.u_address_line_1}}</td>
            <td>{{incident.u_address_post_code}}</td>
          </tr>
        </tbody>

You have to import your HttpClientModule in your app module, code: 您必须在应用模块中导入HttpClientModule,代码为:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpClient,  HttpClientModule } from '@angular/common/http';

import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';

@NgModule({
   imports:      [ BrowserModule, FormsModule, HttpClientModule ],
   providers: [HttpClient, ],
   declarations: [ AppComponent, HelloComponent ],
   bootstrap:    [ AppComponent ]
})
export class AppModule { }

and use data.result in your ngFor loop, check this: 并在ngFor循环中使用data.result,请检查以下内容:

ngOnInit() {
    this.service.getAll().subscribe((data) => {
      this.loading = true;
      this.incidents = data.result;
      this.loading = false;
      console.log('Result - ', data.result);
      console.log('data is recieved');
    })
  }

Can you check if the console is displaying some error ? 您可以检查控制台是否显示一些错误? (Ctrl+Shift+I on Google Chrome, then click on Console) (在Google Chrome上为Ctrl + Shift + I,然后在控制台上单击)

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

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