1

Since I have begin to learn Angular 2 , I am following the basic concepts well. But when I tried running the below program, I don't get the values which is statically set in the code.

import { Component,Input } from '@angular/core';
export class Hero {
  id: number;
  name: string;
}
hero: Hero = {
  id: 1,
  name: 'Windstorm'
};

@Component({
  selector: 'my-app',
 template: '<h1>{{title}}</h1><h2>{{hero.name}} details!</h2><div><label>id: </label>{{hero.id}}</div><div><label>name: </label>{{hero.name}}</div>'

})
export class AppComponent {
  title = 'Tour of Heroes';
  hero = 'Windstorm';
}

Can someone please point out where I am wrong, also can someone show how to debug the Angular code so that I can learn the framework well and try complex things out by self-learning and solve out basic issues by myself?

The output which I got

1 Answer 1

1

This should work

@Component({
  selector: 'my-app',
 template: '<h1>{{title}}</h1><h2>{{hero.name}} details!</h2><div><label>id: </label>{{hero.id}}</div><div><label>name: </label>{{hero.name}}</div>'

})
export class AppComponent {
  title = 'Tour of Heroes';
  hero: Hero = {
    id: 1,
    name: 'Windstorm'
  };
}

With hero = 'Windowstorm'; {{hero.name}} is invalid because hero doesn't have a name property. I'm sure your browser console show an error message. Please add such errors to future questions.

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

5 Comments

I am sorry but the console didn't show up anything.Its clear.
Sorry, you are right. It would only cause an error if hero had been null. It's still helpful to state if questions state whether the console did show errors or not ;-)
Just out of curiosity why would externalizing her class not give me out.Even if the change the hero key to some other name in AppComponent?
Not sure I fully understand the question. Bindings in templates can only refer to properties of the components class instance. hero: Hero = { id: 1, ...} is outside the class and out of reach for the template, thus {{hero}} binds to hero = 'Windstorm';.
Sure I get it. Will learn how to make it in scope by reading some more facts.Thanks for the answer..Cheers.

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.