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

278
Visualizações
Angular 2 ngModel inside a function

I'm trying to pass a ngModel to a function and get the change,

I don't know the way to use.

this is what I got now:

          <ion-input text-right
               formControlName="monday"
               type="number" pattern="[0-9]*"
               placeholder="00.00"
               [(ngModel)]="monday"
               (keypress)="onChange(monday)">
           </ion-input>

           <ion-input text-right
               formControlName="tuesday"
               type="number" pattern="[0-9]*"
               placeholder="00.00"
               [(ngModel)]="tueasday"
               (keypress)="onChange(tuesday)">
           </ion-input>

.... and so on...

Then in my page.ts I got

monday: string = '';
tuesday: string = '';
etc...

onChange(input: string){
//I want the input to correspond to my ngModel so it gets updated
    input = this.clientAction.transformInput(input);
}

I don't want to do:

this.monday = this.clientAction.transformInput(input);

Because as you may think I got all the day of the week so I don't want to have a function for every day like:

onChangeMonday(){};
onChangeTuesday(){};

I need something dynamic.

How can I resolve this issue?

Thanks in advance

[SOLUTION] @AJT_82

instead of using my ngModel and trying to update it, the solution was to access the controls from the form.

in your page.html

      <ion-input text-right
           formControlName="monday"
           type="number" pattern="[0-9]*"
           placeholder="00.00"
           [(ngModel)]="monday"
           (keypress)="onChange(monday, 'monday')">
       </ion-input>

then in your page.ts

onChange(input: string, day: string){
this.rateForm.controls[day].setValue(this.clientAction.transformInput(input));
}

works like a charm now !! Thanks @AJT_82

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Since you have a form, I would suggest that you skip the ngModels altogether and make use of the form you have. Still a bit unsure about what this.clientAction.transformInput(input) is supposed to do, transform the values somehow, as you explained. You should be able to incorporate this, perhaps when you submit the form? Anyway, as said, you have your form values safely stored in the object created by the form, which would for you look something like this:

{
  "monday":null,
  "tuesday":null,
  "wednesday":null,
  // ... and so on
}

When you type in your fields you can capture every keypress with valueChanges, where you can access the complete form:

this.myForm.valueChanges.subscribe(res => {
  console.log("all form values: ", res) // here is an object with all your form values
})

When you need, you can also access each form control by, here accessing monday:

  console.log(this.myForm.controls['monday'].value) 

So this get's rid of the hassle to use ngModel for each form value.

So these are a couple of ways you can intercept the values and then "transform" them at some point, when you want/need to. Probably best place to do so, is when submitting form, loop the values and transform them. And this is totally dynamic, as you wished for, and do not need 7 functions to transform each form control! ;)

Hope this helps you, and here's a plunker (check the console as well).

Plunker

about 4 years ago · Santiago Trujillo Relatório

0

When you are using ngModel why do you want to pass the same in to function

<ion-input text-right
               formControlName="monday"
               type="number" pattern="[0-9]*"
               placeholder="00.00"
               [(ngModel)]="monday"
               (keypress)="onChange()">
           </ion-input>

and handle using the ngModel itself as

onChange(){
    input = this.clientAction.transformInput(this.monday);
}

Update 1:

I got your are looking transform the textbox value as the user types it. Check Custom Directive

about 4 years ago · Santiago Trujillo 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