【发布时间】:2016-07-13 19:33:44
【问题描述】:
我已经为我的注册表单构建了一个验证服务,其中一种静态方法是通过调用我的 API 来检查输入的电子邮件是否可用:
static emailAvailable(control){
let injector = ReflectiveInjector.resolveAndCreate([HTTP_PROVIDERS]);
let http = injector.get(Http);
let valid = "E-mail is available";
http.post('https://secretapi.com/email', JSON.stringify({ email: control.value }))
.map((res: Response) => res.json())
.subscribe(function(result){
if(result.success){
valid = result.success; //The console.log on the line below is correct, the one at the bottom of the script never changes.
console.log(valid);
return null; //Doesn't do anything?
}else{
valid = result.error; //The console.log on the line below is correct, the one at the bottom of the script never changes.
console.log(valid);
return { 'invalidEmailAddress': true }; //Doesn't do anything, just like the return above
}
});
console.log(valid); //Output always "E-mail is available"
}
当电子邮件可用时,它应该向表单验证器返回“null”。底部的最后一个 console.log 应该输出它在订阅调用中收到的消息。这不会发生,我不知道为什么。由于某种原因,订阅调用中发生的所有事情都包含在其中,并且永远不会到达验证器。我应该改变什么?我不知道,现在已经在网上搜索了几个小时。
【问题讨论】:
标签: forms validation http angular subscribe