NativeScript версия 4.2.4 Angular версия 6.0
У меня есть два TextFields на моей странице входа в систему
<StackLayout class="input-field">
<TextField class="input" hint="Username" autocorrect="false" autocapitalizationType="none" [(ngModel)]="user.userName" returnKeyType="next" (returnPress)="focusPassword()"></TextField>
<StackLayout class="hr-light"></StackLayout>
</StackLayout>
<StackLayout class="input-field">
<TextField #password class="input" hint="Password" secure="true" [(ngModel)]="user.password" [returnKeyType]="isLoggingIn ? 'done' : 'next'" (returnPress)="focusConfirmPassword()"></TextField>
<StackLayout class="hr-light"></StackLayout>
</StackLayout>
<StackLayout *ngIf="!isLoggingIn" class="input-field">
<TextField #confirmPassword class="input" hint="Confirm password" secure="true" [(ngModel)]="user.confirmPassword" returnKeyType="done"></TextField>
<StackLayout class="hr-light"></StackLayout>
</StackLayout>
<Button [text]="isLoggingIn ? 'Log In' : 'Sign Up'" (tap)="submit()" class="btn btn-primary m-t-20"></Button>
Все, что я хочу - это получить значения TextField с помощью ngModel.Но я не знаю, почему я не получаю значения, используя user.userName и user.password.Также я пытаюсь использовать приведенный ниже код с использованием двухсторонней привязки данных, но он также не работает.
Вот мой login.component.ts
@Component({
selector: "Login",
moduleId: module.id,
templateUrl: "./login.component.html",
styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
isLoggingIn = true;
user: User;
@ViewChild("password") password: ElementRef;
@ViewChild("confirmPassword") confirmPassword: ElementRef;
constructor(private page: Page, private router: Router, private userService: UserService) {
this.page.actionBarHidden = true;
this.user = new User();
// Use the component constructor to inject providers.
}
toggleForm() {
this.isLoggingIn = !this.isLoggingIn;
}
submit() {
if (this.isLoggingIn) {
this.login();
} else {
// this.register();
}
}
login() {
this.alert("login: Username" + this.user.userName)
this.alert("login: Password" + this.password)
this.userService.login(this.user);
}
focusPassword() {
this.password.nativeElement.focus();
}
focusConfirmPassword() {
if (!this.isLoggingIn) {
this.confirmPassword.nativeElement.focus();
}
}
alert(message: string) {
return alert({
title: "Hashoo Says",
okButtonText: "OK",
message: message
});
}
}
![enter image description here](https://i.stack.imgur.com/R3EAp.jpg)