web-dev-qa-db-fra.com

Angular 5: Module introuvable: Erreur: impossible de résoudre '@ angular / forms / src / validators'

J'essaie de créer un validateur personnalisé à l'aide de la directive mais j'obtiens une erreur inférieure.

ERROR in ./src/app/CustomValidators/white-space-validator.directive.ts
Module not found: Error: Can't resolve '@angular/forms/src/validators' in 'D:\Angular\Admin\src\app\CustomValidators'
resolve '@angular/forms/src/validators' in 'D:\Angular\Admin\src\app\CustomValidators'
  Parsed request is a module
  using description file: D:\Angular\Admin\package.json (relative path: ./src/app/CustomValidators)
    Field 'browser' doesn't contain a valid alias configuration
  after using description file: D:\Angular\Admin\package.json (relative path: ./src/app/CustomValidators)
    resolve as module.....

Code dans le fichier de directives:

import { Directive } from '@angular/core';
import { ValidatorFn, Validator } from '@angular/forms/src/directives/validators';
import { AbstractControl, FormControl } from '@angular/forms/src/model';
import { NG_VALIDATORS } from '@angular/forms/src/validators';

@Directive({
  selector: '[whiteSpace][ngModel]',
  providers: [
    { provide: NG_VALIDATORS, useExisting: WhiteSpaceValidatorDirective, multi: true }
  ]
})

export class WhiteSpaceValidatorDirective  {
  validator : ValidatorFn;

  constructor() { 
    this.validator = checkWhiteSpaces();
  }

  validate(c: FormControl){
    return this.validator(c);
  }
}

function checkWhiteSpaces(): ValidatorFn {
  return (c: AbstractControl) => {
    let isValid = c.value.trim().length > 0 ? true : false;
    if (isValid) {
      return null;
    }
    else {
      return {
        whiteSpace: { valid: false }
      }
    }
  }
}

Versions du package que j'utilise:

  • CLI angulaire: 1.5.5 Nœud: 8.2.1 OS: win32 x64 Angulaire: 5.1.2 ... animations, communes, compilateur, compilateur-cli, noyau, formulaires ... http,
    service de langue, plateforme-navigateur ... plateforme-navigateur-dynamique,
    routeur @ angular/cli: 1.5.5 @ angular-devkit/build-optimizer: 0.0.36 @ angular-devkit/core: 0.0.22 @ angular-devkit/schematics: 0.0.42
    @ ngtools/json-schema: 1.1.0 @ ngtools/webpack: 1.8.5
    @ schematics/angular: 0.1.11 @ schematics/schematics: 0.0.11
    TypeScript: 2.4.2 webpack: 3.8.1

De l'aide ?

5
Abhishek

Importer uniquement à partir de @angular/forms

import { 
      ValidatorFn,
      Validator, 
      AbstractControl, 
      FormControl, 
      NG_VALIDATORS 
} from '@angular/forms';
6
Suren Srapyan