Check out the template for this component:
<form>
<label>Id: </label>
<input type="text" [(ngModel)]="t" (keyup.enter)="Up()">
<label for="pass">Password: </label>
<input type="password" id="pass">
</form>
Here is the TypeScript file for the component:
import { Component } from '@angular/core';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class LoginComponent {
t:string='';
Up(){
console.log(this.t);
}
}
Now, let's take a look at the app.module.ts file:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { LoginComponent } from './login/login.component';
@NgModule({
declarations: [
AppComponent,
LoginComponent
],
imports: [
BrowserModule,
AppRoutingModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
After running the application and entering text into the field and pressing enter, an empty string is printed. Even with FormsModule
added in the app module, the t
variable does not change value. I attempted using t!:string
, but that did not solve the issue either.