Application charges that did not find providers of Http, but is set.
The HttpModule was provided to the app configuration. It was previously recognized as HTTP_PROVIDERS.
My app.module.ts
import { HttpModule } from ‘@angular/http’;
imports: [
HttpModule,
IonicModule.forRoot(MyApp),
],
My page
this.signupForm = this.formBuilder.group({
email: [’’, Validators.compose([ValidationService.checkEmail]) ],
});
According to dependency injection Angular 2 @Injectable invoke the Http method, but this is not a class to be automatically invoked, so Angular 2 provides focus the method invocation through its API. I am using below with ReflectiveInjector method.
My static Validator - ValidationService
import { ReflectiveInjector, Injectable, Injector } from ‘@angular/core’;
import { FormControl } from ‘@angular/forms’;
import { HttpModule, Http, Headers, Response } from ‘@angular/http’;
import {Observable} from “rxjs/Observable”;
import “rxjs/add/observable/fromPromise”;
export interface ValidationResult {
[key: string]: boolean;
}
export class ValidationService {
static checkEmail(control: FormControl): ValidationResult | Observable<Promise<ValidationResult>>{
if (control.value.match(/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/)) {
let injector: Injector = ReflectiveInjector.resolveAndCreate([Http, HttpModule]);
let http: Http = injector.get(Http);
let q = new Promise((resolve) => {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
let user = {
email: control.value
};
http.get('http://localhost:3000/auth/validate-email/'+control.value)
.subscribe(
(res:Response) => function(res){
console.log("valid");
resolve(null);
},
(error) => function() {
console.log("invalid");
console.log(error);
resolve(null);
}
);
});
return Observable.fromPromise(q);
} else {
console.log("invalid");
return { 'invalidEmailAddress': true };
}
}
}