Ich habe einen Validierungsdienst für mein Anmeldeformular und eine der statischen Methoden bauen wird überprüft, ob die eingegebene E-Mail durch den Aufruf meiner API folgend verfügbar:Angular2 Rückgabedaten von Validierungsdienst nach Http rufen
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"
}
Es sollte "Null" an den Formvalidator zurückgeben, wenn die E-Mail verfügbar ist. Das letzte console.log am unteren Rand sollte die Nachricht ausgeben, die es im Abonnementanruf erhält. Das passiert nicht und ich bin mir nicht sicher warum. Aus irgendeinem Grund ist alles, was innerhalb des Subskriptionsaufrufs passiert, dort enthalten und erreicht niemals den Validator. Was soll ich ändern? Ich habe keine Ahnung und habe jetzt stundenlang im Internet gesucht.