为了账号安全,请及时绑定邮箱和手机立即绑定

如何从组件角度获取数据并将其打印到html中?

如何从组件角度获取数据并将其打印到html中?

翻翻过去那场雪 2022-08-18 10:10:58
如何从组件角度获取数据并将其打印到html中?我正在尝试从我的组件中获取我的html中的口袋妖怪名称这是我的 app.components.ts:import { Component, OnInit } from "@angular/core";import { HttpService } from "./http.service";@Component({  selector: "app-root",  templateUrl: "./app.component.html",  styleUrls: ["./app.component.scss"],})export class AppComponent {  title = "pokemon";  tasks = [];  task = "";  constructor(private _httpService: HttpService) {}  ngOnInit() {    this.getTasksFromService();  }  getTasksFromService() {    let observable = this._httpService.getPokemon();    observable.subscribe((data) => {      this.tasks = data["data"];      console.log(data);      return data;    });  }}这是我的HTML:<div *ngFor="let task of tasks; let idx = index">  <p>{{task.name}}</p></div><router-outlet></router-outlet>这是我的http.service.ts:import { Injectable } from "@angular/core";import { HttpClient } from "@angular/common/http";@Injectable({  providedIn: "root",})export class HttpService {  constructor(private _http: HttpClient) {    this.getPokemon();  }  getPokemon() {    let pokemon = this._http.get("https://pokeapi.co/api/v2/pokemon/1/");    return pokemon;  }}
查看完整描述

3 回答

?
炎炎设计

TA贡献1808条经验 获得超4个赞

根据您正在调用的 API


将您的 ts 更新为


getTasksFromService() {

    let observable = this._httpService.getPokemon();

    observable.subscribe((data) => {

      this.tasks = data;

    });

  }

在你的网页中


<div>

  <p>{{task.name}}</p>

</div>

您应该看到输出bulbasaur


当你有口袋妖怪列表时,你需要,但你发布的API(https://pokeapi.co/api/v2/pokemon/1/)包含上述代码工作的单个口袋妖怪fow的详细信息。*ngFor


查看完整回答
反对 回复 2022-08-18
?
眼眸繁星

TA贡献1873条经验 获得超9个赞

修改你的 getTasksFromService as


 getTasksFromService() {

    let observable = this._httpService.getPokemon();

    observable.subscribe((data) => {

      this.tasks = data["abilities"]; //because response data consist of array of ability in 'abilities'

      console.log(data);

      return data;

    });

  }

和你的 html 作为


<div *ngFor="let task of tasks; let idx = index">

  <p>{{task.ability.name}}</p> 

</div>



<router-outlet></router-outlet>

我希望这个解决方案适用于您的问题。


查看完整回答
反对 回复 2022-08-18
?
千万里不及你

TA贡献1784条经验 获得超9个赞

请参阅更新的更改:


componen.ts :


import { Component, OnInit } from "@angular/core";

import { HttpService } from "./http.service";


@Component({

  selector: "app-root",

  templateUrl: "./app.component.html",

  styleUrls: ["./app.component.scss"],

})

export class AppComponent {

  title = "pokemon";

  tasks: any; // Make this as type of any

  task = "";

  constructor(private _httpService: HttpService) {}


  ngOnInit() {

    this.getTasksFromService();

  }


  getTasksFromService() {

    this._httpService.getPokemon.subscribe(data => { // You can directly subscribe to this service, instead of creating an object and then subscribing to it.

      this.tasks = data;

      console.log(data);

    });

  }

}

服务:ts :


import { Injectable } from "@angular/core";

import { HttpClient } from "@angular/common/http";


@Injectable({

  providedIn: "root",

})

export class HttpService {

  constructor(private _http: HttpClient) { }


  getPokemon() {

    return this._http.get("https://pokeapi.co/api/v2/pokemon/1/"); // You can directly return the observable.

  }

}

由于API只返回一个信息,因此您不需要inhtml页面。*ngFor


网页 :


<p>{{task.name}}</p>

我希望这对你有用。


注意:我还提到了根据编码标准进行的变化。


查看完整回答
反对 回复 2022-08-18
  • 3 回答
  • 0 关注
  • 118 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号