[英]Why [(ngModel)] breaks all template in angular 4?
This is my first component in angular. 这是我的第一个角度组件。 Here it is:
这里是:
import { Component } from '@angular/core';
export class Hero{
id:number;
name:string;
}
const HEROES: Hero[]=[
{id:11,name:'Mr. Nice'},
{id:12,name:'Arco'},
{id:13,name:'Gillette'},
{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" (click)="onSelect(hero)">
<span class="badge">{{hero.id}}</span> {{hero.name}}
</li>
</ul>
<h2>Details of {{selectedHero.name}}</h2>
<div><label>Id: </label>{{selectedHero.id}}</div>
<div>
<label>Name: </label>
<input [(ngModel)]="selectedHero.name"/>
</div>
`,
styles: [`
`]
})
export class AppComponent {
title = 'Tour of Heroes';
heroes = HEROES;
selectedHero: Hero;
onSelect(hero: Hero): void {
this.selectedHero = hero;
}
}
I deleted styles
element because its big. 我删除了
styles
元素,因为它很大。 My template doesn't bind heroes
array, When I use this in template
element: 我的模板未绑定
heroes
数组,当我在template
元素中使用此template
:
<input [(ngModel)]="selectedHero.name"/>
But when I delete above input, all heroes
from array are correctly displayed in a <ul>
list. 但是,当我删除上述输入时,来自数组的所有
heroes
都正确显示在<ul>
列表中。
Why is it? 为什么?
You do not set selectedHero
property, but still you try to display and modify it. 您没有设置
selectedHero
属性,但是仍然尝试显示和修改它。 Move hero details to section and add ngIf
to it to display it only when it's set: 将英雄详细信息移至该部分并添加
ngIf
以仅在设置时显示它:
<div *ngIf="selectedHero">
<h2>Details of {{selectedHero.name}}</h2>
<div><label>Id: </label>{{selectedHero.id}}</div>
<div>
<label>Name: </label>
<input [(ngModel)]="selectedHero.name"/>
</div>
</div>
It's because Angular can't read selectedHero.name
while there is no selected hero. 这是因为在没有选定英雄的情况下,Angular无法读取
selectedHero.name
。 You can (and need to) show this input only when you have selectedHero
using *ngIf
directive, like this: 仅当使用
*ngIf
指令selectedHero
*ngIf
,您才能(并且需要)显示此输入,如下所示:
<input *ngIf="selectedHero" [(ngModel)]="selectedHero.name"/>
And not only for input
- for every element where you're using selectedHero
: 不仅用于
input
-对于您使用selectedHero
每个元素:
<h2 *ngIf="selectedHero">Details of {{selectedHero.name}}</h2>
<div *ngIf="selectedHero"><label>Id: </label>{{selectedHero.id}}</div>
<div *ngIf="selectedHero">
<label>Name: </label>
<input [(ngModel)]="selectedHero.name"/>
</div>
Or better add a wrapper with one *ngIf
directive: 或者最好用一个
*ngIf
指令添加一个包装器:
<div *ngIf="selectedHero">
<h2>Details of {{selectedHero.name}}</h2>
<div><label>Id: </label>{{selectedHero.id}}</div>
<div>
<label>Name: </label>
<input [(ngModel)]="selectedHero.name"/>
</div>
</div>
I remember, it was written somewhere in "Tour of Heroes" for Angular, it's not just my thoughts ;) 我记得,它写在《英雄之旅》中的Angular上,不只是我的想法;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.