首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >data.all中的角度8可观测数据获取

data.all中的角度8可观测数据获取
EN

Stack Overflow用户
提问于 2020-01-09 12:12:15
回答 3查看 1.2K关注 0票数 1

我正在使用这个api (https://cat-fact.herokuapp.com/facts)学习角8服务和可观察性。我遇到的问题是我无法访问“所有”中的数据。我使用一个带有http响应的接口。

如果我尝试返回data.all可视代码,请给我一个警告:“IFact[]类型上不存在属性'all‘”。

在视图中,如果我试图访问data.all,就会出现以下错误:

ERROR TypeError:"_co.fact未定义“

接口:

代码语言:javascript
复制
export interface IFact {
    _id: string,
    text: string,
    type: string,
    user: {
        _id: string,
        name: {
            first: string,
            last: string,
        }
    },
    upvotes: number,
    userUpVoted: ''
};

服务:

代码语言:javascript
复制
@Injectable({
  providedIn: 'root'
})
export class FactService {

  private _url = 'http://localhost:4200/facts';

  constructor(private http: HttpClient) { }

  getFacts(): Observable<IFact[]>{
    return this.http.get<IFact[]>(this._url)
  }
}

构成部分:

代码语言:javascript
复制
export class FactComponent implements OnInit {

  public facts = [];

  constructor(private _factService: FactService) { }

  ngOnInit() {

    this._factService.getFacts()
      .subscribe(
        data => {
          this.facts = data;
          console.log(this.facts);
        },
        err => console.log(err)
      )
  }
}

HTML

代码语言:javascript
复制
<table>
    <td ng-repeat="fact of facts">
        <tr>{{fact._id}}</tr>
    </td>
</table>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-01-09 13:00:05

嘿,你的代码中有很多问题。我更新了这段代码,并在您检查的地方创建了堆栈。

组件

代码语言:javascript
复制
import { Component } from "@angular/core";
import { FactService } from "./app.service";

@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent {
  name = "Angular";
  public facts = [];

  constructor(private _factService: FactService) {}

  ngOnInit() {
    this._factService.getFacts().subscribe(
      data => {
        this.facts = data.all;
        console.log(this.facts);
      },
      err => console.log(err)
    );
  }
}

服务

代码语言:javascript
复制
import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { Observable } from "rxjs";
import { IFact } from "./type";

@Injectable({
  providedIn: "root"
})
export class FactService {
  private _url = "https://api.myjson.com/bins/82946";

  constructor(private http: HttpClient) {}

  getFacts(): Observable<IFact> {
    return this.http.get<IFact>(this._url);
  }
}

html

代码语言:javascript
复制
<table>
    <tr *ngFor="let fact of facts">
        <td>{{fact._id}}</td>
    </tr>
</table>

类型

代码语言:javascript
复制
export interface IFact {
  all: Fact[];
}
export interface Fact {
  _id: string;
  text: string;
  type: string;
  user: {
    _id: string;
    name: {
      first: string;
      last: string;
    };
  };
  upvotes: number;
  userUpVoted: "";
}

Stackbliz链接https://stackblitz.com/edit/angular-ubnmuw

票数 1
EN

Stack Overflow用户

发布于 2020-01-09 12:24:17

这可能是在从API返回事实列表之前呈现视图的组件造成的。试试这个:

代码语言:javascript
复制
<table *ngIf="facts && facts.length">
  <td ng-repeat="fact of facts">
    <tr>{{fact._id}}</tr>
  </td>
</table>

编辑

正如注释中所指出的,*ngFor对于角2+更有意义。

代码语言:javascript
复制
<table *ngIf="facts && facts.length">
  <td *ngFor="let fact of facts">
    <tr>{{fact._id}}</tr>
  </td>
</table>
票数 2
EN

Stack Overflow用户

发布于 2020-07-29 05:17:26

日安!下面是示例代码:

代码语言:javascript
复制
Component:

import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { NgZone} from '@angular/core';
import {HttpServiceService} from './../http-service.service';

@Component({
  selector: 'app-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.scss']
})
export class ListComponent implements OnInit {

  constructor(
     private http: HttpServiceService,
     private zone: NgZone
    ) { }
  brews: object;
  ngOnInit(): void {
      this.http.getPeople().subscribe(data => {
      this.brews = data;
      alert(JSON.stringify(this.brews));  // Should diplay joson format
      console.log(this.brews);
      });
  }
}

Service:
import { Observable, of } from 'rxjs';
import { Injectable } from '@angular/core';

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

  constructor() { }
  getPeople(): Observable<any>{
    const peopleArray = [{
      firstName: 'Al',
      lastName: 'Moje',
      age: 63
    }];
    return of(peopleArray);
 }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59663718

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档