我正在尝试在 Angular 2 中设置一个非常基本的服务。目标是在用户单击按钮时显示当前日期和时间。当我使用下面的代码时,我收到以下错误:
Error during evaluation of "click"
ORIGINAL EXCEPTION: TypeError: l_context.whatTime is not a function
时间成分:
@Component({
template: `
<h1>Time Test</h1>
<button (click) = "whatTime()">Time</button>
<h2>{{now}}</h2>
`
})
export class TimeComponent {
now = Date();
constructor(public timeService: TimeService) {
this.now = timeService.whatTime();
}
}
服务:
@Injectable()
export class TimeService {
whatTime() {
return new Date();
}
}
对我做错了什么有任何想法吗?