nativescript-angular 7.1 - double data binding is not working (both tns preview and tns run android)
Environment
Provide version numbers for the following components (information can be retrieved by running tns info in your project folder or by inspecting the package.json of the project):
- CLI: 5.1.1
- Cross-platform modules:
- Android Runtime: 5.1.0
- iOS Runtime: 5.1.0
- Plugin(s): "nativescript-theme-core": "~1.0.4", "nativescript-ui-sidedrawer": "~5.1.0", "nativescript-ui-listview": "~5.0.1",
- NativeScript-Angular: "nativescript-angular": "~7.1.0",
- Angular: "@angular/animations": "~7.1.0", "@angular/common": "~7.1.0", "@angular/compiler": "~7.1.0", "@angular/core": "~7.1.0", "@angular/forms": "~7.1.0", "@angular/http": "~7.1.0", "@angular/platform-browser": "~7.1.0", "@angular/platform-browser-dynamic": "~7.1.0", "@angular/router": "~7.1.0",
Describe the bug
I use the playground tutorial Sample-Groceries. I downloaded the code sample in view to run Nativscript on my own Android device. I have an issue with the double way binding [(ngModel)]="user.email" on the login page.
As specified in the tutorial, I set the User object email and password in the login component's constructor.
import { Component, OnInit } from "@angular/core";
import { Router } from "@angular/router";
import { Page } from "tns-core-modules/ui/page";
import { UserService } from "../shared/user/user.service";
import { User } from "../shared/user/user.model";
@Component({
selector: "Login",
providers: [UserService],
moduleId: module.id,
templateUrl: "./login.component.html",
styleUrls: ["./login.component.css"]
})
export class LoginComponent implements OnInit {
user: User;
isLoggingIn = true;
constructor(private router: Router, private userService: UserService, private page: Page) {
this.user = new User();
this.user.email = "[email protected]";
this.user.password = "value1";
}
ngOnInit() {
this.page.actionBarHidden = true;
}
submit() {
if (this.isLoggingIn) {
this.login();
} else {
this.signUp();
}
}
login() {
this.userService.login(this.user)
.subscribe(
() => this.router.navigate(["/home"]),
(error) => alert("Unfortunately we could not find your account.")
);
}
signUp() {
this.userService.register(this.user)
.subscribe(
() => {
alert("Your account was successfully created.");
this.toggleDisplay();
},
() => alert("Unfortunately we were unable to create your account.")
);
}
toggleDisplay() {
this.isLoggingIn = !this.isLoggingIn;
}
changeData(){
this.user.email = "[email protected]";
this.user.password = "aaa";
}
}
the one way bindings works :
<Label [text]="user.email"></Label>
<Label [text]="user.password"></Label>
but the two way binding is not working at all:
<TextField hint="Email Address" keyboardType="email" autocorrect="false" autocapitalizationType="none" [(ngModel)]="user.email" class="input input-border"></TextField>
<TextField hint="Password" secure="true" [(ngModel)]="user.password" class="input input-border"></TextField>
I even created a function to update the data from the component:
changeData(){
this.user.email = "[email protected]";
this.user.password = "aaa";
}
Once again the one way binding does update the label text but not the [(ngModel)].
Also, I cannot log in as the [(ngModel)] does not bind the typed value back to the component this.user.
Is there anything wrong with my configuration or with the Angular 7 version ?
Here is the login.html:
<FlexboxLayout>
<StackLayout class="form" [class.dark]="!isLoggingIn">
<TextField hint="Email Address" keyboardType="email" autocorrect="false"
autocapitalizationType="none" [(ngModel)]="user.email" class="input input-border"></TextField>
<TextField hint="Password" secure="true" [(ngModel)]="user.password"
class="input input-border"></TextField>
<Label [text]="user.email"></Label>
<Label [text]="user.password"></Label>
<Button text="changeData" class="btn btn-primary" (tap)="changeData()"></Button>
<Button [text]="isLoggingIn ? 'Sign in' : 'Sign up'" class="btn btn-primary"
(tap)="submit()"></Button>
<Button [text]="isLoggingIn ? 'Sign up' : 'Back to login'" (tap)="toggleDisplay()"></Button>
</StackLayout>
</FlexboxLayout>
here is my app.module.ts
import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
import { NativeScriptModule } from "nativescript-angular/nativescript.module";
import { NativeScriptHttpModule } from "nativescript-angular/http";
import { NativeScriptFormsModule } from "nativescript-angular/forms";
import { NativeScriptUISideDrawerModule } from "nativescript-ui-sidedrawer/angular";
import { NativeScriptUIListViewModule } from "nativescript-ui-listview/angular";
import { AppRoutingModule } from "./app-routing.module";
import { AppComponent } from "./app.component";
@NgModule({
bootstrap: [
AppComponent
],
imports: [
AppRoutingModule,
NativeScriptModule,
NativeScriptHttpModule,
NativeScriptFormsModule,
NativeScriptUISideDrawerModule,
NativeScriptUIListViewModule
],
declarations: [
AppComponent
],
schemas: [
NO_ERRORS_SCHEMA
]
})
export class AppModule { }
here is my full package.json:
{
"nativescript": {
"id": "org.ecoLyon.app",
"tns-android": {
"version": "5.1.0"
},
"tns-ios": {
"version": "5.1.1"
}
},
"description": "NativeScript Application",
"license": "SEE LICENSE IN <your-license-filename>",
"repository": "<fill-your-repository-here>",
"scripts": {
"lint": "tslint \"src/**/*.ts\""
},
"dependencies": {
"@angular/animations": "~7.1.0",
"@angular/common": "~7.1.0",
"@angular/compiler": "~7.1.0",
"@angular/core": "~7.1.0",
"@angular/forms": "~7.1.0",
"@angular/http": "~7.1.0",
"@angular/platform-browser": "~7.1.0",
"@angular/platform-browser-dynamic": "~7.1.0",
"@angular/router": "~7.1.0",
"nativescript-angular": "~7.1.0",
"nativescript-theme-core": "~1.0.4",
"nativescript-ui-sidedrawer": "~5.1.0",
"nativescript-ui-listview": "~5.0.1",
"reflect-metadata": "~0.1.12",
"rxjs": "~6.3.0",
"tns-core-modules": "~5.1.0",
"zone.js": "~0.8.26"
},
"devDependencies": {
"@angular/compiler-cli": "~7.1.0",
"@nativescript/schematics": "~0.5.0",
"@ngtools/webpack": "~7.1.0",
"codelyzer": "~4.5.0",
"nativescript-dev-sass": "~1.6.0",
"nativescript-dev-typescript": "~0.7.0",
"nativescript-dev-webpack": "~0.19.0",
"tslint": "~5.11.0"
},
"gitHead": "9b6565a4513375a33a59c44262dfc1d818a8205e",
"readme": "EcoLyon Application"
}
Hi @mquentin,
I tested the described issue in a sample app, however, was unable to recreate the problem. You can find the project here. When you build the app make some changes in the TextField and tap on the Show result button.
Hi @tsonevn, Thanks for spending som time to analyse my issue.
On the preview, when I tap your onButtonTap function, it does alert either [email protected] value1 or [email protected] aaa after I tap the changeData function.

Hi @mquentin, Thank you for the additional info. I have just tested the sample-Groceries project from the repository that you have mentioned above, however, I am still unable to recreate the issue. The only change that I have made in the project is to replace
submit() {
if (!this.user.isValidEmail()) {
alert("Enter a valid email address.");
return;
}
this.isAuthenticating = true;
if (this.isLoggingIn) {
this.login();
} else {
this.signUp();
}
}
with
submit() {
alert("Username: "+this.user.email+" Pass: "+this.user.password);
}
inside the login/login.component.ts. I am also attaching an video, which demostrates the behavior on my side.
It will help if you can send us your project or make the needed changes in the project here. This will help in the investigation.
demo.mp4.zip
Hi, I restarted my test project from scratch from the sample-Groceries instead of using my playground tutorial downloaded code. I don't have the binding issue anymore... Thanks for your time and your responsiveness.
I know the issue is closed, but I'm having the same issue with playground downloaded app. So, start from scratch!
Facing same Issue with 7.1
<TextField [(ngModel)]="piece" hint="Pieces"></TextField>
<TextField [(ngModel)]="diamond" hint="Diamond"></TextField>
<Button text="Total" class="-primary" (tap)="calculate();"></Button>