我有以下代码:
import { AbstractControl, ValidationErrors } from '@angular/forms';
import { Observable } from 'rxjs/Observable';
import { filter, map, tap} from 'rxjs/operators';
export function valueInCollectionValidator(collection: Observable<any[]>, propertyName: string) {
return (control: AbstractControl): Observable<ValidationErrors> => {
return collection.pipe(
filter(array => array.find(e => e[propertyName] == control.value)),
map(ok => ok ? undefined : { valueNotInCollection: false }),
);
};
}
使用angular CLI&n;服务时,我收到了构建错误:
validators.ts中的错误(9,29):属性&#39;发现&#39;类型&#39; {}&#39;。
上不存在这是我的jsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"module": "commonjs",
"lib": [
"es2017",
"dom"
]
}
}
知道为什么这不起作用?谢谢!
答案 0 :(得分:1)
filter
方法自动接受每个数组元素。实际上,您在array
方法中引用的filter
是数组collection
的元素,在您的情况下是object
。