Can you help me to make it works, please?
The two-way binding is not getting/updating the values.
I would like that the values in app.component.ts were shown in app.component.html.
Thanks!
I will post the files:
app.component.html:
<div>
<form>
<button type="button">Enviar</button><br>
<label for="txt_usuario">Usuario:</label>
<input type="text" id="txt_usuario" title="Usuário" size="10" [(ngModel)]="usuario"><br>
<label for="txt_senha">Senha:</label>
<input type="text" id="txt_senha" title="Senha" size="10" [(ngModel)]="senha"><br>
</form>
<span>Usuario: {{usuario}}</span><br>
<span>Senha : {{senha}}</span>
</div>
app.component.ts:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
title: string = 'teste';
usuario: string = 'X';
senha: string = '123';
send(usuario: string, senha: string) {
console.log(usuario);
console.log(senha);
}
}
app.module.ts:
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';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
As you are using it inside a form you can use formsModule for the two way data binding
or if you want to make them standalone:
I added a project so you can see this working: https://stackblitz.com/edit/angular-zdtdww?file=src%2Fmain.ts