1

I'm new to Angular and I'm trying to follow the tutorial on https://angular.io/tutorial. My problem is that in chrome in my watch list, I can't see the const HEROES. It says "not available". What am I missing? It's possible to see it in the watch list?

import { Component } from '@angular/core';

export class Hero {
  id: number;
  name: string;
}

const HEROES: Hero[] = [
  { id: 11, name: 'Mr. Nice' },
  { id: 12, name: 'Narco' },
  { id: 13, name: 'Bombasto' },
  { id: 14, name: 'Celeritas' },
  { id: 15, name: 'Magneta' },
  { id: 16, name: 'RubberMan' },
  { id: 17, name: 'Dynama' },
  { id: 18, name: 'Dr IQ' },
  { id: 19, name: 'Magma' },
  { id: 20, name: 'Tornado' }
];

@Component({
  selector: 'my-app',
  template: `
    <h1>{{title}}</h1>
    <h2>My Heroes</h2>
    <ul class="heroes">
      <li *ngFor="let hero of heroes"
        [class.selected]="hero === selectedHero"
        (click)="onSelect(hero)">
        <span class="badge">{{hero.id}}</span> {{hero.name}}
      </li>
    </ul>
    <div *ngIf="selectedHero">
      <h2>{{selectedHero.name}} details!</h2>
      <div><label>id: </label>{{selectedHero.id}}</div>
      <div>
        <label>name: </label>
        <input [(ngModel)]="selectedHero.name" placeholder="name"/>
      </div>
    </div>
  `,
  styles: [`...

export class AppComponent {
  title = 'Tour of Heroes';
  heroes = HEROES;
  selectedHero: Hero;

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

1 Answer 1

2

When your website ist fully rendered you lose the closure in which your variable is. If you still want to debug the variable, you can use the debugger line in javascript or set a breakpoint manually. eg.

var a = b;
debugger;

Then open DevTools and you should land in the debugger breakpoint.

Most devs use a tool like https://augury.angular.io/ to debug their apps.

Sign up to request clarification or add additional context in comments.

3 Comments

"lose the closure" means the variable is dead? If so, I don't understand how the app is capable of changing values in it.
developer.mozilla.org/en/docs/Web/JavaScript/Closures You are in a different context then the variable you are watching. See the first example: Imagine you are in a debugger after line 8: Even though there will be an alert saying 'Mozilla', the variable is defined in another function, if you have a watcher on that variable it will be undefined, because it is not in the 'root' closure, but inside another closure.
Got it. Now I just have to get use to it :).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.