2017-06-15 5 views
0

関数内でajaxが機能しないようですが、ここで何が間違っていますか?関数内でIonic HTTPが機能しない

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 
      }); 
     }); 
     } 

    }); 

    } 

} 

私が手にエラーがある:コンストラクタメソッドに引数としてそれを追加してみてください

constructor(_backend: ConnectionBackend, _defaultOptions: RequestOptions) 

https://angular.io/api/http/Http

enter image description here

答えて

0

new Http()コールは、2つのパラメータが必要ですあなたのクラスのために:

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); 
+0

それでもコンストラクタクラスとなし、外出先:http://cdn.microthosting.com/imgs/

export class EmailValidator { contstructor($http: Http) {} ... 

また、ブートストラップコードにHttpModuleをインポートするために、このようなものが必要Screenshot_at_2017-06-15_12-02-58.png http://cdn.microthosting.com/imgs/Screenshot_at_2017-06-15_12-03-13.png – jfreak53

+0

これは、クラスが正しく設定されていないことを意味します。あなたのクラスに 'Http'クラスを注入する前に、真実である必要があるいくつかのことがあります。このチュートリアルを確認してください:https://angular.io/guide/http –

+0

この記事では、「HttpModule」の詳細について説明します:https://medium.com/google-developer-experts/angular-2-introduction-to-new -http-module-1278499db2a0 –

関連する問題