代码之家  ›  专栏  ›  技术社区  ›  Ahmer Ali Ahsan

NativeScript Angular 6双向绑定在TextField上不起作用

  •  2
  • Ahmer Ali Ahsan  · 技术社区  · 6 年前

    NativeScript版本4.2.4

    角度版本6.0

    这是我在nativescript上的第一个应用程序。我熟悉angular,所以我需要选择nativescript而不是ionic来构建本机混合应用程序。

    脚本

    我用sidekick创建了这个应用程序。现在,首先我创建了两个TextField并用一些字符串属性绑定它们的文本。在这里,我告诉大家,这个模板默认使用延迟加载,这个应用程序中的所有组件都有单独的module.ts文件。

    问题

    应用模块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 { }
    

    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 { }
    

    主页.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();
        }
    }
    

    主页.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

    输出

    enter image description here

    2 回复  |  直到 6 年前
        1
  •  4
  •   William Juan    6 年前

    你需要使用 [(ngModel)] 将其绑定到另一个文本字段

    <TextField [(ngModel)]="TestingText"></TextField>
    

    [text] 把它绑在标签上

    <Label [text]="TestingText"></Label>
    
        2
  •  1
  •   D C    6 年前

    https://play.nativescript.org/?template=play-ng&id=76g6B9

    主页.component.html

    <TextField [(ngModel)]="textFieldData"></TextField>
    
    <Label [text]="labelData"></Label>
    
    <TextView [(ngModel)]="textViewData"></TextView>
    

    主页.component.ts

    textViewData: string = "hey this is text view";
    labelData: string = "hey this is label";
    textFieldData: string = "hey this is text field";