2016-09-21 35 views
2

我是從角2官方網站嘗試英雄教程:物業成AppComponent返回空值角2

https://angular.io/docs/ts/latest/tutorial/toh-pt1.html

但我不能檢索AppComponent的值,它總是返回一個空值。

這裏app.component.ts

import { Component } from '@angular/core'; 
@Component({ 
    selector: 'my-app', 
    template: '<h1>Titulo: {{titulo}}</h1>' 
}) 
export class AppComponent { 
    titulo: 'Tour de heroes'; 
} 

的代碼時,我保存更改並刷新頁面,我只得到:TITULO:

回答

2

應該titulo = 'Tour de heroes',不titulo: 'Tour de heroes'

:用於指定變量類型,=用於賦值給變量。例如:

titulo: string = 'Tour de heroes' 

上述代碼聲明string類型的可變titulo並分配值Tour de heroes給它。