NativeScript Angular 6 双向绑定在 TextField 上不起作用
NativeScript Angular 6 two way binding doesn't work on TextField
简介
NativeScript 版本 4.2.4
Angular 版本 6.0
这是我在 nativescript 上的第一个应用程序。我熟悉 angular,因此我需要选择 nativescript 而不是 ionic 来构建原生混合应用程序。
场景
我使用 Sidekick 创建了这个应用程序。现在,首先我创建了两个 TextField 并将它们的文本与一些字符串 属性 绑定。在这里我告诉大家,这个模板默认使用延迟加载,这个应用程序中的所有组件都有单独的 module.ts 文件。
问题
当我 运行 智能 phone 上的应用程序(实时同步)绑定后。双向数据绑定不起作用。我在这里分享我的代码。
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>
此外我还发现了下面的相关问题
但这对我来说不起作用。我认为我的问题是相同的,但在技术上有所不同。这个问题是基于 angular 2 但在我的例子中我使用的是 angular 6.
输出
您需要使用 [(ngModel)]
将其绑定到另一个文本字段
<TextField [(ngModel)]="TestingText"></TextField>
或[text]
将其绑定到标签
<Label [text]="TestingText"></Label>
这里是 playground link 您需要的工作代码
https://play.nativescript.org/?template=play-ng&id=76g6B9
home.component.html
<TextField [(ngModel)]="textFieldData"></TextField>
<Label [text]="labelData"></Label>
<TextView [(ngModel)]="textViewData"></TextView>
home.component.ts
textViewData: string = "hey this is text view";
labelData: string = "hey this is label";
textFieldData: string = "hey this is text field";
简介
NativeScript 版本 4.2.4
Angular 版本 6.0
这是我在 nativescript 上的第一个应用程序。我熟悉 angular,因此我需要选择 nativescript 而不是 ionic 来构建原生混合应用程序。
场景
我使用 Sidekick 创建了这个应用程序。现在,首先我创建了两个 TextField 并将它们的文本与一些字符串 属性 绑定。在这里我告诉大家,这个模板默认使用延迟加载,这个应用程序中的所有组件都有单独的 module.ts 文件。
问题
当我 运行 智能 phone 上的应用程序(实时同步)绑定后。双向数据绑定不起作用。我在这里分享我的代码。
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>
此外我还发现了下面的相关问题
但这对我来说不起作用。我认为我的问题是相同的,但在技术上有所不同。这个问题是基于 angular 2 但在我的例子中我使用的是 angular 6.
输出
您需要使用 [(ngModel)]
将其绑定到另一个文本字段
<TextField [(ngModel)]="TestingText"></TextField>
或[text]
将其绑定到标签
<Label [text]="TestingText"></Label>
这里是 playground link 您需要的工作代码
https://play.nativescript.org/?template=play-ng&id=76g6B9
home.component.html
<TextField [(ngModel)]="textFieldData"></TextField>
<Label [text]="labelData"></Label>
<TextView [(ngModel)]="textViewData"></TextView>
home.component.ts
textViewData: string = "hey this is text view";
labelData: string = "hey this is label";
textFieldData: string = "hey this is text field";