gpt4 book ai didi

css - 我怎样才能让第一个英雄被选为angular2的初始条件?

转载 作者:太空宇宙 更新时间:2023-11-03 21:14:13 25 4
gpt4 key购买 nike

在angular2英雄教程中

template: `
<h1>{{title}}</h1>
<h2>My Heroes</h2>
<ul class="heroes">
<li *ngFor="let hero of heroes"
[class.selected]="hero === selectedHero"
(click)="onSelect(hero)">
<div *ngIf="hero ==heroes[0]"> first hero </div>
<span class="badge">{{hero.id}}</span> {{hero.name}}
</li>
</ul>
<hero-detail [hero]="selectedHero"></hero-detail>`

export class AppComponent implements OnInit {
title = 'Tour of Heroes';
heroes: Hero[];
selectedHero: Hero;

constructor(private heroService: HeroService) { }

getHeroes(): void {
this.heroService.getHeroes().then(heroes => this.heroes = heroes);
}

ngOnInit(): void {
this.getHeroes();
}

onSelect(hero: Hero): void {
this.selectedHero = hero;
}
}

如何在第一次启动时选择第一个英雄元素我想在单击另一个英雄元素时更改 selectedHero

最佳答案

this.heroService.getHeroes().then(heroes => {
this.heroes = heroes;
this.selectedHero = this.heroes[0];
});

关于css - 我怎样才能让第一个英雄被选为angular2的初始条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43675064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com