【问题标题】:Angular 8 ngFor not bidingAngular 8 ngFor不绑定
【发布时间】:2020-11-24 03:59:40
【问题描述】:

我有英雄联盟冠军 API 女巫返回我从 Champions.service 获得的内容。

.ts已更新

    
    @Component({   selector: 'app-champions',   templateUrl: './champions.component.html',   styleUrls: ['./champions.component.css'] })
    
    export class ChampionsComponent implements OnInit {
    
      public champions;   
      public arrayChampions;  
      
         constructor(private championsService:ChampionsService) { }
    
      ngOnInit(): void {
        this.getAllChampions();   }
    
     getAllChampions(){
    this.championsService.getChampions().subscribe(
      data => { this.champions = data, 
        this.arrayChampions = Object.entries(this.champions.data).map(([k,v]) => ({ [k]:v })),
        this.ArrayIterator(); 
      },
      err => {console.error(err)},
      () => console.log("Champions cargados")
    );
  }

  ArrayIterator() {
    let IteratableArray = Array();
    for (let item of Object.keys(this.arrayChampions[0])) {
      var eventItem = Object.values(this.arrayChampions[0]);
      IteratableArray.push(eventItem);
    }
    this.arrayChampions = IteratableArray[0];
  }
    
    
    }

champions.service

    import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders} from '@angular/common/http';
 
    const httpOptions = {   
     headers: new HttpHeaders({'Content-Type': 'application/json'}) 
    }
 
    @Injectable({   providedIn: 'root' }) export class ChampionsService {
 
    constructor(private http: HttpClient) { }
 
    getChampions(){
     return this.http.get('http://ddragon.leagueoflegends.com/cdn/10.23.1/data/es_ES/champion.json');
    }

HTML:

    <div *ngFor="let arrayChampion of arrayChampions>
        <a class="text-decoration-none">{{arrayChampion.id}}</a>
    </div>

事实上,HTML 中没有任何内容。我不太清楚是哪个问题。

【问题讨论】:

  • 您在*ngFor 循环结束时错过了结束"
  • 同样在初始加载时arrayChampions.id 为空,因此不会显示任何内容,您可能需要在运行getAllChampions 后刷新您的dom,您在最初加载页面时是否遇到任何控制台错误?
  • 您能否也从championsService 发布您的代码,以便我们了解您是如何检索数据的?
  • 冠军服务已添加。我已经更正了 *ngFor 的 en 处的 "

标签: arrays json angular


【解决方案1】:

你的数组第一个元素是object,因为你不能直接在模板中迭代它。因此,您需要相应地对其进行转换,以便能够遍历模板。为此,您可以继续以下操作:

从这里开始,我将附上工作中的 stackblitz 示例 here

示例 HTML:

<div *ngFor="let arrayChampion of arrayChampions>
    <a class="text-decoration-none">{{arrayChampion.id}}</a>
</div>

示例组件.ts:

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

@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent implements OnInit {
  name = "Angular 4";
  arrayChampions: any;
  data = [
    {
      Aatrox: {
        id: "Aatrox",
        key: "266"
      },
      Ahri: {
        id: "Ahri",
        key: "103"
      },
      Akali: {
        id: "Akali",
        key: "84"
      }
    }
  ];

  constructor() {}
  ngOnInit() {
    this.ArrayIterator();
  }

  ArrayIterator() {
    let IteratableArray = Array();
    for (let item of Object.keys(this.data[0])) {
      var eventItem = Object.values(this.data[0]);
      IteratableArray.push(eventItem);
    }
    this.arrayChampions = IteratableArray[0];
  }
}

【讨论】:

  • 您好,感谢您的帮助。我已经复制了你在 stackblitz 示例中所做的,但现在我只获得了第一个冠军。我添加了 Champions.service
  • 在继续您的最新一期之前,您能否将其标记为正确并给予支持?
  • 感谢您的支持。您可以在另一个线程中分享您的最新问题吗?
  • 我已经更新了那里的答案。请将其标记为 coorect 并投反对票。如果有什么需要澄清的,请给我评论。 @罗伯特
猜你喜欢
  • 2018-02-03
  • 1970-01-01
  • 2020-06-21
  • 2019-11-01
  • 2023-03-30
  • 1970-01-01
  • 2020-09-28
  • 2017-07-30
  • 1970-01-01
相关资源
最近更新 更多