首页 文章

从类中双向数据绑定nativescript

提问于
浏览
0

如何在Nativescript中实现双向绑定?

以下是我的尝试 . 变量CompModel包含值“FA I Test” .

我希望能够以类别FA I Test“设置的方式绑定数据,然后在用户单击按钮时更改要显示的值的任何值 .

从@ angular / core导入;
从“ui / core / dependency-observable”导入
;
从“data / observable”导入
;

@零件({
选择器:“我的应用程序”,
模板:<ActionBar title =“我的应用2”> </ ActionBar> <! - 你的UI组件在这里 - > <StackLayout> <TextView [(ngModel)] ='CompModel.Name'> </ TextView> <按钮文字=“点击”(点按)=“点击()”> </按钮> </ StackLayout>
})
export class AppComponent {
//你的TypeScript逻辑就在这里
public CompModel:Plain;

构造(){
this.CompModel = new Plain();
this.CompModel.Name =“FA I TEST”;
}
点击(OBJ){
警报(this.CompModel.Name);
}
}

出口类平原{
public name:String;
构造(){
}
}

1 回答

  • 1

    确保您已注册 NativeScriptFormsModule

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

相关问题