用
<div>
将模板的HTML英雄细节内容包装起来。然后添加ngIf内置指令并将其设置为组件的selectedHero属性。ngIf对Angular Heroes教程没有价值
<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>
该应用程序不再失败和名称列表在浏览器中再次显示。
事实是,没有ngIf指令,应用程序永远不会失败。为了测试这一点,我清空了输入表单,这反过来清空了绑定的列表项,但无论是否包含ngIf,都不会发生任何奇怪的事情。
任何解释?
您确定控制台中没有错误吗?因为'selectedHero.name'应该会失败,如果'selectedHero'未定义或为空。 –
@MikeMcCaughan零错误 – usefulBee
然后我想这是教程中的一个错误。你应该告诉Angular团队...... –