ngModel 值在与输入一起使用时未定义

问题描述 投票:0回答:1

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

我得到未定义的时间戳值。我在这里遗漏了什么吗?

angular input angular-ngmodel
1个回答
0
投票

一切都按预期工作。您没有将

timestamp: any
初始化为任何东西,所以它是
undefined
.

Change

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

现场演示

© www.soinside.com 2019 - 2024. All rights reserved.