Reputation: 4601
Why do I get an error in this code ( which you must be familiar from the angular docs as the code is mostly the code there )
The error stems from the line with the stars...
import { Component } from '@angular/core';
export class Hero {
id: number;
name: string;
}
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Tour of Heroes';
hero: Hero = {
id: 1,
name: 'Windstorm'
};
if (hero.name === 'prekazi') { // <--******************
// do something
}
}
The error is
Failed to compile.
/Users/.../Desktop/angular-tour-of-heroes/src/app/app.component.ts (20,11): ',' expected.
It's due to this line:
if (hero.name === 'prekazi') {
Why can't we get to the property of the hero instance with the dot notation? Could someone kindly point me what I'm missing here?
Upvotes: 0
Views: 27
Reputation: 10516
Your code is inside the class, but not inside a method of the class. For example, it works if you add it to ngOnInit
:
export class AppComponent {
title = 'Tour of Heroes';
hero: Hero = {
id: 1,
name: 'Windstorm'
};
ngOnInit() {
if (this.hero.name === 'prekazi') { // <--******************
alert('That\'s my hero! ')
}
}
}
Upvotes: 1