Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

261
Visualizações
Angular - Manually restricting max value of input using ngModel and ngModelChange

I'm trying to manually restrict user from enter a value in input more than a certain value. I'm using Angular 9 and ngModel on the input.

As you can see in the below code, on ngModelChange, if the input value for age is larger than 27, I'm setting it to 27.

If the user inputs a bigger number like 144, it displays 27 in the input box as soon as the user types the last 4. But for numbers that have 27 as the prefix, e.g. 271, 2700, etc., the value displayed in input box is not changed. Does anyone know why and is there a way to have it display correctly?

Please note that the variable age is set correctly to 27 for all larger numbers. Just that the input box display does not change for numbers that have 27 as the prefix.

Here is the Codepen link: https://codepen.io/baggasumit/pen/MWVgaqZ

@Component({
  selector: 'my-app',
  template: `
   <h1>ngModel Example</h1>
   <label>Age </label>
   <input type="number" max="27" [ngModel]="age" (ngModelChange)=onAgeChange($event) />
   <br>
   <br>
   <div>Age is set to {{age}}</div>
  `
})
class AppComponent {
  public age;
  
  public onAgeChange(newAge) {
    if (newAge > 27) {
      this.age = 27;
    } else {
      this.age = newAge;
    }
    console.log(this.age);
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to also update the html element input's #myInput value.

const { Component, VERSION } = ng.core;

@Component({
  selector: 'my-app',
  template: `
   <h1>ngModel Example</h1>
   <label>Age </label>
   <input type="number" #myInput [(ngModel)]="age" (ngModelChange)=onAgeChange(myInput) />
   <br>
   <br>
   <div>Age is set to {{age}}</div>
  `
})
class AppComponent {
  public age;
  
  public onAgeChange(myInput) {
    if (this.age > 27) {
      this.age = 27;
    } 
    myInput.value = this.age
    console.log(this.age);
  }
}


// main.js
const { BrowserModule } = ng.platformBrowser;
const { NgModule } = ng.core;
const { CommonModule } = ng.common;
const { FormsModule } = ng.forms;

@NgModule({
  imports: [
    BrowserModule,
    CommonModule,
    FormsModule,
  ],
  declarations: [AppComponent],
  bootstrap: [AppComponent],
  providers: []
})
class AppModule {}

const { platformBrowserDynamic } = ng.platformBrowserDynamic; 

platformBrowserDynamic()
  .bootstrapModule(AppModule)
  .catch(err => console.error(err));
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda