html:
<input [(ngModel)]="timestamp">
<button (click)="getInstantDetails()">display</button>
组件.ts
timestamp: any;
getInstantDetails(): void {
console.log(this.timestamp);
}
app.module.ts
import {FormsModule} from '@angular/forms';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule,
FormsModule,
],
providers: [SchedulerService],
bootstrap: [AppComponent]
})
export class AppModule { }
我得到未定义的时间戳值。我在这里遗漏了什么吗?
一切都按预期工作。您没有将
timestamp: any
初始化为任何东西,所以它是 undefined
.
Change
timestamp: string = 'hello';
和 getInstantDetails() 将记录 'hello'
.