解决这一问题的最佳方法是通过双向数据绑定。您需要做的第一件事是添加
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>
最后,确保
_fname
和
_lname
表单中的属性
.ts
文件
export class SomeComponent {
_fname = "";
_lname = "";
save() {
console.log(this._fname);
console.log(this._lname);
// Send values to your DB
}
}