Jehad Ahmad Jaghoub
Jehad Ahmad Jaghoub

Reputation: 1383

inject http or service inside static

I am making validation on angular2 through GlobalValidaitor.ts file i put on it all extra method that i need as following

import { FormControl } from '@angular/forms';
import { Http } from '@angular/http';
import { Api } from '../providers/api';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';


export class GlobalValidator {


   constructor(public Api: Api) {

  }



  public  static checkEmailExisit(control: FormControl): any {
     return new Promise(resolve => {

      //Fake a slow response from server

      let  senddata: {email?: string} = {
        email : control.value
      };


      let seq = this.Api.post('phones/offers', senddata).share() ;
    seq
     .map(res => res.json())
     .subscribe(res => {
     resolve(res);
     });

    });

  }



  static mailFormat(control: FormControl): any {

    var EMAIL_REGEXP = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/i;

    if (control.value != "" && (control.value.length <= 5 || !EMAIL_REGEXP.test(control.value))) {
        return { "incorrectMailFormat": true };
    }

    return null;
}




}

and as its a static don't consider API i will be thankful to help in angular 2 not the last version it shall be usage of HTTP_PROVIDER and inject directly onside the function but for the last version of angular it don't work i event cant import HTTP_PROVIDER

Upvotes: 0

Views: 3205

Answers (2)

Jehad Ahmad Jaghoub
Jehad Ahmad Jaghoub

Reputation: 1383

i was possible to inject http inside static i do it as following

import { FormControl } from '@angular/forms';
import { ReflectiveInjector } from '@angular/core';
import { Http,Headers, XHRBackend, ConnectionBackend, BrowserXhr, ResponseOptions, XSRFStrategy, BaseResponseOptions, CookieXSRFStrategy, RequestOptions, BaseRequestOptions } from '@angular/http';



export class GlobalValidator extends CookieXSRFStrategy{



   static checkUserEmail(control: FormControl): any {
    let data :any ;
     let http =  ReflectiveInjector.resolveAndCreate([
             Http, BrowserXhr,
             { provide: ConnectionBackend, useClass: XHRBackend },
             { provide: ResponseOptions, useClass: BaseResponseOptions },
             { provide: XSRFStrategy, useClass: GlobalValidator },
             { provide: RequestOptions, useClass: BaseRequestOptions }
           ]).get(Http);


    var body = 'email='+control.value;
     var headers = new Headers();
     headers.append('Content-Type', 'application/x-www-form-urlencoded');

    let reques=  http
       .post('http://www.etc.cc/ChekEmail',
         body, {
           headers: headers
         }).share() ;


      reques   .map(res => res.json())
         .subscribe(res => {
           console.error('connected :', res.ishere);


         }, err => {
             console.error('ERROR in connection :',  JSON.stringify(err));
           });

  }

any way thanks for trying help

Upvotes: -1

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657676

The best approach would be to not use static at all.

Remove static and pass the validator like

@NgModule({
  providers: [GlobalValidator],
})
constructor(private globalValidator:GlobalValidator) {}
  ... 
  myEmail: [globalValidator.emailExists.bind(globalValidator)]
  ...

Upvotes: 2

Related Questions