I am trying to create a custom Angular 2 form Validatorto check if a user exists in the database.
This is the code of my user form Validator
import { FormControl } from '@angular/forms';
import {API} from "../services/api";
import {ReflectiveInjector} from "@angular/core";
export class EmailValidator {
constructor() {}
static checkEmail(control: FormControl,): any {
let injector = ReflectiveInjector.resolveAndCreate([API]);
let api = injector.get(API);
return api.checkUser(control.value).then(response => {
response;
});
}
}
And this is my special service that is responsible for the request to the node api on the backend
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class API {
private backendUrl = 'http://127.0.0.1:5000/api/register/';
constructor(private http: Http) { }
checkUser(email:string): Promise<any> {
return this.http.get(this.backendUrl + email)
.toPromise()
.then(response => response.json())
.catch(this.handleError);
}
When I try to check the user, this is an error that is displayed
EXCEPTION: Error in ./TextInput class TextInput - inline template:0:0 caused by: No provider for Http! (API -> Http)
What am I doing wrong?
thank
Hanzo source
share