我正在尝试为模型驱动表单创建异步自定义验证指令 指令
export default class AsyncValidator implements Validator { constructor(private fetchService: FetchService) { } validate(c: AbstractControl) { return this.validateUniqueNameObservable(c.value); } validateUniqueNameObservable(name: string) { return this.fetchService.fetchName(name).subscribe(observer => { if (name === observer) { observer.next({ asyncInvalid: true }); } else { observer.next(null); } }) } }
验证运行时有一个例外" EXCEPTION:_this.subscribe不是函数"。有人可以指出我在观察和订阅方面做错了什么吗?
修改 - 服务
public fetchName(name:string) {
var result = this.http.get('/api/project/checkName/' +name).map(response => response.json());
return result;
}
的元器件
export class CreateComponent implements OnInit {
public project: Project;
public createForm: FormGroup;//md form
constructor(private http:Http, private fb:FormBuilder) {
}
ngOnInit(): void {
this.createForm = this.fb.group({
name: ['', [<any>Validators.required, <any>Validators.maxLength(150)]],
client: ['', [<any>Validators.maxLength(150)]],
notes: ['', [<any>Validators.maxLength(255)]]
});
}
我正在尝试为名称格式控件创建异步自定义验证,它将调用fetchservice来检查db中是否存在名称并返回一个observable。