我正在写一个angular2验证器指令。我有以下情况:
有一个数字A,而且有一个数字B,我需要验证B字段中的数字是否小于或等于A字段中的数字,我使用的是模板驱动表格。
指令中的代码是
import { Directive, Input, OnChanges, SimpleChanges } from '@angular/core';
import { AbstractControl, NG_VALIDATORS, Validator, ValidatorFn, Validators } from '@angular/forms';
export function validateLesserOrEqual(number: number): ValidatorFn {
console.log(number);
return (c: AbstractControl) => {
return c.value <= number ? null : {
validateLesserOrEqual: {
valid: false
}
}
}
}
@Directive({
selector: '[lesserOrEqual][ngModel]'
})
export class LesserOrEqualValidator implements Validator, OnChanges {
@Input() lesserOrEqualInput: number;
private _validator = Validators.nullValidator;
ngOnChanges(changes: SimpleChanges) {
const change = changes['lesserOrEqual'];
if(change) {
const val: number = change.currentValue;
this._validator = validateLesserOrEqual(val);
}
else {
this._validator = Validators.nullValidator;
}
}
validate(c: AbstractControl): {[number: number]: any} {
return this._validator(c);
}
}
在我的输入中我有以下[lesserOrEqual]="movement.parcel_number"
,但是当我把方括号括在指令周围时,我得到了以下错误Unhandled Promise rejection: Template parse errors:
Can't bind to 'lesserOrEqual' since it isn't a known property of 'input'. ("dirty}"