我似乎无法让ajax在我的函数内部工作,我在这里做错了什么?
import { FormControl } from '@angular/forms';
import { Http } from '@angular/http';
export class EmailValidator {
static checkEmail ( control: FormControl ): any {
var $http = new Http;
return new Promise(resolve => {
var lowercaseValue = control.value.toLowerCase();
if(lowercaseValue.indexOf('@') <= -1 || lowercaseValue.indexOf('.') <= -1) {
resolve({
"invalid email": true
});
} else {
$http({
method: 'GET',
url: '{URL}'+lowercaseValue
}).then(function successCallback(response) {
if(response.error == true) {
resolve(response);
} else {
resolve(null);
}
}, function errorCallback(response) {
resolve({
"invalid email": true
});
});
}
});
}
}
我得到的错误是:
答案 0 :(得分:0)
new Http()
来电需要2个参数:
constructor(_backend: ConnectionBackend, _defaultOptions: RequestOptions)
https://angular.io/api/http/Http
尝试将其作为参数添加到您的类的构造函数方法中:
export class EmailValidator {
contstructor($http: Http) {}
...
您还需要将HttpModule
导入到引导代码中,如下所示:
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
@NgModule({
imports: [ BrowserModule, HttpModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
class AppModule { }
platformBrowserDynamic().bootstrapModule(AppModule);