I am doing the Tour of heroes tutorial. When I add the following piece of code to my heroes.component.ts file I get an error that says "Property 'selectedHero' has no initializer and is not definitely assigned in the constructor." What should i do so that I don't see this error anymore? Thank you in advance.
selectedHero: Hero;
onSelect(hero: Hero): void {
this.selectedHero = hero;
}
My heroes.component.ts file looks like this:
import {Hero} from '../hero';
import {HEROES} from '../mock-heroes';
@Component({
selector: 'app-heroes',
templateUrl: './heroes.component.html',
styleUrls: ['./heroes.component.css']
})
export class HeroesComponent implements OnInit{
heroes=HEROES;
selectedHero:Hero;
constructor() {}
ngOnInit(): void {
}
onSelect(hero: Hero) : void {
this.selectedHero = hero;
}
My heroes.component.html file looks like this:
<ul class="heroes">
<li *ngFor= "let hero of heroes" (click)= "onSelect(hero)">
<span class="badge">{{hero.id}}</span>{{hero.name}}
</li>
</ul>
<h2>{{selectedHero.name | uppercase}} Details</h2>
<div><span>id:</span>{{selectedHero.id}}</div>
<div>
<label>name:
<input [(ngModel)]= "selectedHero.name" placeholder="name">
</label>
</div>