当与输入一起使用时,ngModel值将变为未定义

时间:2018-03-29 09:01:29

标签: angular input angular-ngmodel

HTML:

    <input [(ngModel)]="timestamp">
    <button (click)="getInstantDetails()">display</button>

component.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 { }

我的时间戳值未定义。这里有什么我想念的吗?

1 个答案:

答案 0 :(得分:0)

一切都按预期工作。您没有将timestamp: any初始化为undefined

更改timestamp: string = 'hello';和getInstantDetails()将记录'hello'

Live demo