Hey there, I'm just getting started with Angular 2 and I've been working on creating a basic form using FormBuilder
and FormGroup
. However, for some reason, the value I input into the username field in my view is not updating in my component. When I click the submit button, I don't see the new value reflected. Below is the code for both my view and component - could someone please help me identify what might be causing this issue?
login.ts
import { Component} from "@angular/core";
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
@Component({
selector:"login-page",
templateUrl: "login.html"
})
export class LoginPage {
loginForm : FormGroup;
constructor(formBuilder :FormBuilder) {
this.loginForm = formBuilder.group({
'username':[
'',
Validators.required
]
});
}
validate() : boolean {
if (this.loginForm.valid) {
return true;
}
}
submit() : void {
if (!this.validate()) {
console.info("Invalid input")
}
let control = this.loginForm.controls['username'].value;
console.log(control);
}
}
login.html
<ion-header style="text-align:center">
<ion-navbar>
<ion-title>Please login</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<h3 style="text-align:center">Welcome to your first Ionic app!</h3>
<form class ="list" [formGroup]="loginForm" (ngSubmit)="submit()">
<ion-item class="loginInput center">
<ion-label floating>Email</ion-label>
<ion-input type="text" name='username' ngControl="username" ></ion-input>
</ion-item>
<div class="loginBtn"><button style="width: 140px" ion-button type="submit">Login</button></div>
</form>
</ion-content>