简体   繁体   English

Angular 8 ng For notbidding

[英]Angular 8 ngFor not biding

I have the League of Legends Champion API witch returns what I'm getting from the champions.service.我有英雄联盟冠军 API 女巫返回我从 Champions.service 获得的信息。

.ts Updated .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: HTML:

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

The fact is that nothing appears in the HTML.事实是 HTML 中没有任何内容。 I don't know really well which is the problem.我不太清楚这是什么问题。

Your array first element is an object since because of that you cannot iterate it straightly in the template.您的数组第一个元素是一个object ,因为您无法直接在模板中对其进行迭代。 Therefore you need to transform it accordingly to be able to iterate over through the template.因此,您需要相应地对其进行转换,以便能够遍历模板。 For that you can proceed with the following :为此,您可以继续执行以下操作:

From here with I am attaching the working stackblitz example here从这里我附上工作stackblitz例子在这里

Sample HTML :示例 HTML :

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

Sample component.ts :示例 component.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];
  }
}

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

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