【发布时间】:2018-08-22 09:51:59
【问题描述】:
我有以下代码:
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 的 ng serve 时,出现构建错误:
validators.ts (9,29) 中的错误:类型“{}”上不存在属性“find”。
这是我的 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"
]
}
}
知道为什么这不起作用吗?谢谢!
【问题讨论】:
-
我真正在寻找的是一种将数组的可观察对象“展平”为单个项目的可观察对象的方法。这可以通过使用 mergeAll() 运算符解决,并在此处回答:stackoverflow.com/questions/42482705/…
标签: angular typescript ecmascript-6 angular-cli