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

238
Visualizações
Directive not working for non numeric values

I have directive to forbid entering non-numeric symbols in input

Here is directive

  import { NgControl } from "@angular/forms";
import { HostListener, Directive } from "@angular/core";

    @Directive({
      exportAs: "number-directive",
      selector: "number-directive, [number-directive]",
    })
    export class NumberDirective {
      private el: NgControl;
      constructor(ngControl: NgControl) {
        this.el = ngControl;
      }
      // Listen for the input event to also handle copy and paste.
      @HostListener("input", ["$event.target.value"])
      onInput(value: string): void {
        // Use NgControl patchValue to prevent the issue on validation
        this.el.control.patchValue(value.replace(/[^0-9]/g, "").slice(0));
      }
    }

Here is how I use it on input

  <input
  type="text"
  tabindex="0"
  class="search__input form-control-md + {{class}}"
  [value]="config.value"
  [required]="config.required"
  [attr.maxlength]="config.maxLength"
  [attr.minLength]="config.minLength"
  #inputElem
  number-directive
/>

But I still can write aaaaa or any words

Where can be my problem?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Suspect two root causes:

  1. Forgot to add NumberDirective to declarations for app.module.ts.

    You need to add NumberDirective to declarations for app.module.ts to register the directive.

@NgModule({
  ...
  declarations: [..., NumberDirective],
})
export class AppModule {}
  1. No provider forNgControl for your <input> element.

    You will get the error message as below as missing [(ngModel)] for the input element. The directive expects that it is a NgControl. Hence it needs to have [(ngModel)].

    Or you need [formControl]="configControl" for your <input> element.

Error: R3InjectorError(AppModule)[NgControl -> NgControl -> NgControl]: NullInjectorError: No provider for NgControl!

<input
  type="text"
  tabindex="0"
  class="search__input form-control-md + {{ class }}"
  [value]="config.value"
  [required]="config.required"
  [attr.maxlength]="config.maxLength"
  [attr.minLength]="config.minLength"
  #inputElem
  number-directive
  [(ngModel)]="config.value"
/>

OR

<input
  type="text"
  tabindex="0"
  class="search__input form-control-md + {{ class }}"
  [value]="config.value"
  [required]="config.required"
  [attr.maxlength]="config.maxLength"
  [attr.minLength]="config.minLength"
  #inputElem
  number-directive
  [formControl]="configControl"
/>
configControl = new FormControl();

Sample Solution on StackBlitz

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