angular - 角度控制匹配未知
问题描述
我有一个 Angular 自定义指令,如下所示:
import { ValidatorFn, AbstractControl } from '@angular/forms';
export function numericValidator(): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } | null => {
const testNumericPattern = '/^[0-9]{1,6}/g';
if (!control.value) {
return null;
}
const isValidNumeric = control.value.match(testNumericPattern);
if (!isValidNumeric) {
return { value: true };
}
return null;
};
我的测试如下:
describe('NumericValidatorDirective', () => {
const customNumber = new FormControl('', [numericValidator()]);
it('should gives error when number is less than 0', () => {
customNumber.patchValue(-1);
expect(customNumber.valid).toBeFalsy();
});
fit('should gives error when a number is more than 999999', () => {
customNumber.patchValue(999999);
expect(customNumber.valid).toBeFalsy();
});
});
但问题是,当我运行测试时,出现以下错误:
TypeError:control.value.match 不是函数
尽管我已经删除了检查if (!control.value) {
,但我在测试时仍然遇到同样的问题。
知道为什么会出现这个问题,因为它对我来说似乎不合逻辑吗?
解决方案
推荐阅读
- c++ - 为什么输出不同而公式相同?
- c# - 如何在 C# 中单击元素
- sql-server - 在 SQL 中将总和作为列重复
- excel - 根据条件计算平均值
- python - odoo 12 中树视图上的计算字段
- html - 带有可滚动内容的固定页眉和页脚+不可点击的超链接
- powershell - Powershell命令Register-PSRepository以错误结束
- javascript - 禁用从全局错误函数调用本地错误 - JQuery
- python - 尽管有 100% 的覆盖率报告,如何找到在 coverage.py 中从未执行过的代码
- c# - 获取 LinkButton 嵌入式标签值