【发布时间】: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 处的 "