Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
Inyectar servicio personalizado en un validador personalizado

Estoy tratando de hacer un Validator de formulario Angular 2 personalizado para verificar si existe un usuario en una base de datos.

Este es el código de mi formulario personalizado 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; }); } }

Y este es mi servicio personalizado que es responsable de realizar la solicitud a una API de nodo en el 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); }

Cuando trato de validar a un usuario este es el error que se muestra

 EXCEPTION: Error in ./TextInput class TextInput - inline template:0:0 caused by: No provider for Http! (API -> Http)

¿Qué estoy haciendo mal?

Gracias

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

@Injectable() export class EmailValidator { constructor(private api:API) {} /* static */ checkEmail(control: FormControl,): any { return this.api.checkUser(control.value).then(response => { response; }); } }

Agréguelo a @NgModule() o @Component() según el alcance que desee que tenga

 providers: [EmailValidator]

Inyéctalo en el componente donde quieras usarlo.

 export class MyComponent { constructor(private emailValidator:EmailValidator, fb:FormBuilder){} this myForm = fb.group({ email: [], [this.emailValidator.checkEmail.bind(this.emailValidator)] }); }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!