Search code examples
angularnativescriptangular2-nativescriptnativescript-angular

NativeScript Angular 6 two way binding doesn't work on TextField


Introduction

NativeScript version 4.2.4

Angular version 6.0

This is my first app on nativescript. I'm familiar with angular, so I need to choose nativescript over ionic for building native hybrid apps.

Scenario

I have created this app using sidekick. Now, first I created two TextField and bind their text with some string property. Here I tell you peoples that this template is using lazy loading by default and all components in this app have separate their module.ts files.

Problem

After binding when I run app (live sync) on my smart phone. Two way data binding doesn't work. Here I'm sharing my code.

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>

Furthermore I've also found below related question

Binding doesnt work on textfield

but this doesn't work in my case. I think my question is same but different in technology. This question is based on angular 2 but in my case I'm using angular 6.

Output

enter image description here


Solution

  • You would need to use [(ngModel)] to bind it to another text field

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

    or [text] to bind it to a label

    <Label [text]="TestingText"></Label>