Angular2: Cannot read property 'name' of undefined

匿名 (未验证) 提交于 2019-12-03 01:49:02

问题:

I am beginning to learn Angular2. I've been following the Heroes Tutorial provided at angular.io. All was working fine until, being annoyed by the clutter of HTML using the template, I used template URL in its place, and moved the HTML to a file named hero.html. The error that is generated is, "Cannot read property 'name' of undefined". Strangely, the heroes variable which points to an array of objects can be accessed so that ngFor will produce the correct amount of "li" tags according to the number of objects in the array. However, the data of the objects of the array cannot be accessed. Furthermore, even a simple variable holding some text, will not display using {{}} brackets in the HTML (see provided code).

app.component.ts

import { Component } from '@angular/core'; @Component({   selector: 'my-app',   templateUrl: './hero.html',   styleUrls:['./styles.css'] })  export class AppComponent {   title = 'Tour of Heroes';   heroes = HEROES;   selectedHero:Hero;    onSelect(hero: Hero):void{       this.selectedHero = hero;   } }  export class Hero{    id: number;    name: string; }  const HEROES: Hero[] = [    { id: 1, name: 'Mr. Nice' },    { id: 2, name: 'Narco' },    { id: 3, name: 'Bombasto' },    { id: 4, name: 'Celeritas' },    { id: 5, name: 'Magneta' },    { id: 6, name: 'RubberMan' },    { id: 7, name: 'Dynama' },    { id: 8, name: 'Dr IQ' },    { id: 9, name: 'Magma' },    { id: 10, name: 'Tornado' } ];

hero.html

{{title}}

My Heroes

  • {{hero.id}} {{hero.name}}

{{hero.name}} details!

{{hero.id}}

Here is a photo:

enter image description here

回答1:

The variable selectedHero is null in the template so you cannot bind selectedHero.name as is. You need to use the elvis operator for this case:

The separation of the [(ngModel)] in [ngModel] and (ngModelChange) is also needed because you can't assign to an expression that uses the elvis operator.

I also think you mean to use:

{{selectedHero?.name}} details!

instead of:

{{hero.name}} details!



回答2:

You just needed to read a little further and you would have been introduced to the *ngIf structural directive.

selectedHero.name doesn't exist yet because the user has yet to select a hero so it returns undefined.

{{selectedHero.name}} details!

{{selectedHero.id}}

The *ngIf directive keeps selectedHero off the DOM until it is selected and therefore becomes truthy.

This document helped me understand structural directives.



回答3:

You were getting this error because you followed the poorly-written directions on the Heroes tutorial. I ran into the same thing.

Specifically, under the heading Display hero names in a template, it states:

To display the hero names in an unordered list, insert the following chunk of HTML below the title and above the hero details.

followed by this code block:

My Heroes

It does not instruct you to replace the previous detail code, and it should. This is why we are left with:

{{hero.name}} details!

outside of our *ngFor.

However, if you scroll further down the page, you will encounter the following:

The template for displaying heroes should look like this:

My Heroes

  • {{hero.id}} {{hero.name}}

Note the absence of the detail elements from previous efforts.

An error like this by the author can result in quite a wild goose-chase. Hopefully, this post helps others avoid that.



回答4:

This line

{{hero.name}} details!

is outside *ngFor and there is no hero therefore hero.name fails.



回答5:

This avoid this, you could as well initialize the selectedHero member of your component to an empty object (instead of leaving it undefined).

In your example code, that would give something like this :

export class AppComponent {   title = 'Tour of Heroes';   heroes = HEROES;   selectedHero:Hero = new Hero();    onSelect(hero: Hero):void{       this.selectedHero = hero;   } } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!