Angular 5下的自定义验证(密码检查)未执行

Angular 5下的自定义验证(密码检查)未执行,angular,validation,Angular,Validation,我正在尝试为Angular 5模板表单创建自定义密码验证程序。常规验证(如required、maxlength等)正在工作,但自定义验证代码没有工作 从我看到的情况来看,文件apxPaswordcheck.ts中的验证器代码没有被执行。我已将console.logprint语句放置在自定义验证代码apxPaswordcheck.ts中,但似乎没有打印任何内容 我能做些什么来解决这个问题 短暂性脑缺血发作 当前角度版本 OS: win32 x64 Angular: 5.1.1 ... animat

我正在尝试为Angular 5模板表单创建自定义密码验证程序。常规验证(如
required
maxlength
等)正在工作,但自定义验证代码没有工作

从我看到的情况来看,文件apxPaswordcheck.ts中的
验证器
代码没有被执行。我已将
console.log
print语句放置在自定义验证代码apxPaswordcheck.ts中,但似乎没有打印任何内容

我能做些什么来解决这个问题

短暂性脑缺血发作

当前角度版本

OS: win32 x64
Angular: 5.1.1
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

@angular/cli: 1.6.1
@angular-devkit/build-optimizer: 0.0.38
@angular-devkit/core: 0.0.23
@angular-devkit/schematics: 0.0.42
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.1
@schematics/angular: 0.1.13
typescript: 2.4.2
webpack: 3.10.0
目录设置

apxPasswordcheck.ts

import { Directive, forwardRef } from '@angular/core';
import { NG_VALIDATORS, AbstractControl, ValidatorFn, Validator, FormControl, FormGroup } from '@angular/forms';

// validation function
function validateApxPasswordCheckFactory() : ValidatorFn {

    console.log("TEST -> got in to the area");
    return (group: FormGroup) => {

    let pass = group.controls.password.value;
    console.log("TEST -> PASS " + pass);

    let confirmPass = group.controls.passwordconf.value;
    console.log("TEST -> CONFIRMPASS " + confirmPass);

    let isValid = pass === confirmPass;

    if (isValid) {
        return null;
      } else {
        return {
          apxPasswordcheck: {
            valid: false
          }
        };
      }
    }    
  }

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

export class ApxPasswordCheckValidator implements Validator {
  validator: ValidatorFn;

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

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

}
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { HttpErrorResponse } from '@angular/common/http';
import { FormGroup, FormBuilder, Validators, FormControl } from '@angular/forms';
import { ElementRef } from '@angular/core/src/linker/element_ref';

import { ApxPasswordCheckValidator } from '../../../apxPaswordcheck';

@Component({
  selector: 'app-admin-register-form',
  templateUrl: './admin-register-form.component.html',
  styleUrls: ['./admin-register-form.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class AdminRegisterFormComponent implements OnInit {

  firstname = '';
  lastname = '';
  email = '';
  username = '';
  password = '';
  passwordconf = '';

  constructor() { }

  ngOnInit() {

  }


  ngAfterViewInit() {
    document.getElementById('preloader').classList.add('hide');
  }


  onSubmit(form: ElementRef) {
    console.log("form was submitted");
    console.log(form);
  }
}
管理员注册表单.component.html的密码区域

<div class="row">
  <div class="col-xs-12 col-sm-6 col-md-6">

    <!-- set up password -->
    <div class="form-group">
      <div class="input">
        <label>Password</label>
        <input type="password" [(ngModel)]="password" id="password" class="form-control input-lg" name="password" #userPassword="ngModel"
          [ngClass]="{'is-invalid' : userPassword.errors && userPassword.touched }" minlength="8" tabindex="1"
          required apxPasswordcheck>
        <div class="invalid-feedback">
          Password must be at least 8 characters long
        </div>
      </div>
    </div>
  </div>


  <div class="col-xs-12 col-sm-6 col-md-6">

    <!-- set up password confirmation -->
    <div class="form-group">
      <div class="input">
        <label>Password Confirmation</label>
        <input type="password" [(ngModel)]="passwordconf" id="passwordconf" class="form-control input-lg" name="passwordconf" #userPasswordconf="ngModel"
          [ngClass]="{'is-invalid' : userPasswordconf.errors && userPasswordconf.touched }" minlength="8" tabindex="1"
          required apxPasswordcheck>
        <div class="invalid-feedback">
          Password confirmation is not equal to the Password
        </div>

        <p *ngIf="userPasswordconf.apxPasswordcheck && userPasswordconf.touched">
          Password and CONFIRMATION must be the same
        </p>

      </div>
    </div>
  </div>
</div>