来自 class 的两种数据绑定 nativescript

Two way data binding nativescript from class

如何在 Nativescript 中实现 TWO Way 绑定?

下面是我试过的。变量 CompModel 包含一个值 "FA I Test"。

我希望能够以两种方式绑定数据,即在 class 级别 FA I 测试中设置的方式”,然后用户在单击按钮时在文本框中更改的任何值都是显示.

    import { Component } from "@angular/core";
import { DependencyObservable } from "ui/core/dependency-observable";
import { Observable } from "data/observable";


@Component({
  selector: "my-app",
  template: `
    <ActionBar title="My App 2"></ActionBar>
    <!-- Your UI components go here -->
    <StackLayout>
    <TextView [(ngModel)]='CompModel.Name'></TextView>
    <Button text="Click" (tap)="Clicked()"></Button>
    </StackLayout>
  `
})
export class AppComponent {
  // Your TypeScript logic goes here
  public CompModel: Plain;

  constructor(){
    this.CompModel = new Plain();
    this.CompModel.Name= "FA I TEST";
  }
  Clicked(obj){
    alert(this.CompModel.Name);
  }
}

export class Plain {
  public Name : String;
  constructor(){
  }
}

确保您已注册 NativeScriptFormsModule:

import { NativeScriptFormsModule } from "nativescript-angular/forms"

@NgModule({
    ...
    imports: [
        NativeScriptModule,
        AppRoutingModule,
        NativeScriptFormsModule // <-- this will enable 2-way binding
    ],
    ...
})