【发布时间】:2016-07-07 02:09:53
【问题描述】:
在 Angular2 中创建组件类时。为什么我们不需要var?声明新变量时?例如:
@Component({
selector: 'my-app',
template: `
<h1>{{title}}</h1>
`
})
export class AppComponent {
title = 'My title';
}
怎么不是var title = 'My title';呢?
编辑:当我阅读this 时,我的原始问题出现了,原始代码如下所示:
import { Component } from '@angular/core';
export class Hero {
id: number;
name: string;
}
@Component({
selector: 'my-app',
template: `
<h1>{{title}}</h1>
<h2>{{hero.name}} details!</h2>
<div><label>id: </label>{{hero.id}}</div>
<div>
<label>name: </label>
<input [(ngModel)]="hero.name" placeholder="name">
</div>
`
})
export class AppComponent {
title = 'Tour of Heroes';
hero: Hero = {
id: 1,
name: 'Windstorm'
};
}
【问题讨论】:
标签: typescript angular variable-declaration