简介
NativeScript 版本 4.2.4
Angular 版本 6.0
这是我在 nativescript 上的第一个应用程序。我熟悉 Angular,因此我需要选择 nativescript 而不是 ionic 来构建原生混合应用程序。
场景
我使用 sidekick 创建了这个应用程序。现在,首先我创建了两个 TextField 并将它们的文本与一些字符串属性绑定(bind)。在这里我告诉大家,这个模板默认使用延迟加载,这个应用程序中的所有组件都有单独的 module.ts 文件。
问题
当我在智能手机上运行应用程序(实时同步)时绑定(bind)后。双向数据绑定(bind)不起作用。我在这里分享我的代码。
app.module.ts
import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
import { NativeScriptModule } from "nativescript-angular/nativescript.module";
import { NativeScriptUISideDrawerModule } from "nativescript-ui-sidedrawer/angular";
import { NativeScriptFormsModule } from "nativescript-angular/forms";
import { AppRoutingModule } from "./app-routing.module";
import { AppComponent } from "./app.component";
@NgModule({
bootstrap: [
AppComponent
],
imports: [
AppRoutingModule,
NativeScriptModule,
NativeScriptFormsModule,
NativeScriptUISideDrawerModule
],
declarations: [
AppComponent
],
schemas: [
NO_ERRORS_SCHEMA
]
})
export class AppModule { }
home.module.ts
import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
import { NativeScriptCommonModule } from "nativescript-angular/common";
import { NativeScriptFormsModule } from "nativescript-angular/forms";
import { HomeRoutingModule } from "./home-routing.module";
import { HomeComponent } from "./home.component";
@NgModule({
imports: [
NativeScriptCommonModule,
NativeScriptFormsModule,
HomeRoutingModule
],
declarations: [
HomeComponent
],
schemas: [
NO_ERRORS_SCHEMA
]
})
export class HomeModule { }
home.component.ts
import { Component, OnInit } from "@angular/core";
import { RadSideDrawer } from "nativescript-ui-sidedrawer";
import * as app from "tns-core-modules/application";
@Component({
selector: "Home",
moduleId: module.id,
templateUrl: "./home.component.html"
})
export class HomeComponent implements OnInit {
TestingText: string;
constructor() {
this.TestingText = 'anything';
// Use the component constructor to inject providers.
}
ngOnInit(): void {
// Init your component properties here.
}
onDrawerButtonTap(): void {
const sideDrawer = <RadSideDrawer>app.getRootView();
sideDrawer.showDrawer();
}
}
home.component.html
<ActionBar class="action-bar">
<!--
Use the NavigationButton as a side-drawer button in Android
because ActionItems are shown on the right side of the ActionBar
-->
<NavigationButton ios:visibility="collapsed" icon="res://menu" (tap)="onDrawerButtonTap()"></NavigationButton>
<!--
Use the ActionItem for IOS with position set to left. Using the
NavigationButton as a side-drawer button in iOS is not possible,
because its function is to always navigate back in the application.
-->
<ActionItem icon="res://navigation/menu" android:visibility="collapsed" (tap)="onDrawerButtonTap()" ios.position="left">
</ActionItem>
<Label class="action-bar-title" text="Home"></Label>
</ActionBar>
<FlexboxLayout class="page">
<StackLayout class="form">
<StackLayout class="input-field">
<TextField hint="Type Something" [(ngModel)]="TestingText" secure="false" class="input input-border"></TextField>
<StackLayout class="hr-light"></StackLayout>
</StackLayout>
<StackLayout class="input-field">
<TextField hint="Binding" Text="{{TestingText}}" secure="false" class="input input-border"></TextField>
<StackLayout class="hr-light"></StackLayout>
</StackLayout>
</StackLayout>
</FlexboxLayout>
此外,我还发现了以下相关问题
Binding doesnt work on textfield
但这对我来说不起作用。我认为我的问题是相同的,但在技术上有所不同。这个问题基于 Angular 2,但在我的例子中,我使用的是 Angular 6。
输出
最佳答案
您需要使用 [(ngModel)]
将其绑定(bind)到另一个文本字段
<TextField [(ngModel)]="TestingText"></TextField>
或[text]
将其绑定(bind)到标签
<Label [text]="TestingText"></Label>
关于angular - NativeScript Angular 6 双向绑定(bind)在 TextField 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52673015/