如何从HTML文件到TypeScript文件获取TextField值?

如何从HTML文件到TypeScript文件获取TextField值?,html,angular2-nativescript,Html,Angular2 Nativescript,我不熟悉nativescript。我想在手机中存储用户数据。为此,我使用了Couchbase数据库。现在,我的要求是在单击save按钮时获取TextField值` <TextField hint=" firstName " [text]="_fname "> </TextField> <TextField hint="lastname " [text]="_lname "> </TextField> <button (tap)="sav

我不熟悉nativescript。我想在手机中存储用户数据。为此,我使用了Couchbase数据库。现在,我的要求是在单击save按钮时获取TextField值`

<TextField hint=" firstName " [text]="_fname ">

</TextField>
<TextField hint="lastname " [text]="_lname ">

</TextField>

<button (tap)="save()" class="btn btn-primary active" text="Save"></button>

`

在上面,我需要在单击按钮时获得两个文本字段值。
请解释如何从textfield访问当前值。提前感谢。

解决此问题的最佳方法是通过双向数据绑定。您需要做的第一件事是将
NativeScriptFormsModule
添加到导入的
NgModule
列表中,如下所示

应用程序模块.ts

import { NgModule } from "@angular/core";
import { NativeScriptFormsModule } from "nativescript-angular/forms";
import { NativeScriptModule } from "nativescript-angular/nativescript.module";

import { AppComponent } from "./app.component";

@NgModule({
  imports: [
    NativeScriptModule,
    NativeScriptFormsModule
  ],
  declarations: [AppComponent],
  bootstrap: [AppComponent]
})
export class AppModule {}
然后需要更新组件
.html
文件以使用双向数据绑定。这会将指定元素绑定到组件的.ts文件中的属性

<TextField hint=" firstName " [(ngModel)]="_fname "> </TextField>
<TextField hint="lastname " [(ngModel)]="_lname "> </TextField>

<button (tap)="save()" class="btn btn-primary active" text="Save"></button>

正是我发现了我的问题。我忘了添加app.module.ts。现在它开始工作了。没有人解释我们必须在app.modules.ts中进行更改。谢谢你的回答。我忘了在app.module.ts中添加这一行从“nativescript/forms”导入{NativeScriptFormsModule};
export class SomeComponent {
    _fname = "";
    _lname = "";

    save() {
        console.log(this._fname);
        console.log(this._lname);
        // Send values to your DB
    }
}