Saroj Gurung
Saroj Gurung

Reputation: 15

Angular needs a structure to follow?

I am following the https://angular.io/tutorial/toh-pt2 tutorial to learn about angular. And apparently if the "Hero" class and "const HEROES" array are not above the "@Component" section, the application does not work. Any reason behind this? Is there a structure that needs to be followed?

Upvotes: 0

Views: 50

Answers (1)

BeetleJuice
BeetleJuice

Reputation: 40936

Javascript is parsed top to bottom. So when the constant HERORES or the class Hero is referenced inside the component, they must already be known to the Javascript engine. Otherwise it does not recognize them and throws an error.

// ok
var name = 'Anna';
console.log(name);

// error: what's 'name'?
console.log(name);
var name = 'Anna'

it's not an Angular specific issue.

Upvotes: 0

Related Questions