【问题标题】:ngModel value is coming as undefined when used with inputngModel 值与输入一起使用时未定义
【发布时间】:2018-03-29 09:01:29
【问题描述】:

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】:

    一切都按预期工作。您没有将 timestamp: any 初始化为任何东西,所以它的 undefined

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

    Live demo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-20
      • 2019-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-02
      • 1970-01-01
      • 2018-10-02
      相关资源
      最近更新 更多