2017-06-16 12 views
6

Tôi đang học Ionic 3 và tôi gặp phải lỗi này khi cố gắng tạo trình xác thực tùy chỉnh để kiểm tra duy nhất tên người dùng. Tôi đã cố hết sức nhưng không thể giải quyết vấn đề này.Cách giải quyết "Lỗi: Không bắt buộc (trong lời hứa): Lỗi: Không có nhà cung cấp cho" lỗi trong Ionic 3

CustomValidators.ts

import { Directive, Input } from '@angular/core'; 

import { FormControl, Validator, AbstractControl } from '@angular/forms'; 
import { Http, Response, Headers, RequestOptions } from '@angular/http'; 
import {Observable} from 'rxjs/Rx'; 

export class CustomValidators { 


    constructor(public http: Http){} 

     public hasUniqueEmail(
     control: FormControl, 
    ){ 

     return this.http.get('assets/users.json') 
     .map(res => res.json()) 
     .catch((error:any) => Observable.throw(error.json().error || 'Server error')); 

     } 


} 

signup.ts

import { Component } from '@angular/core'; 
import { IonicPage, NavController, NavParams } from 'ionic-angular'; 
import { FormBuilder, FormGroup, Validators } from '@angular/forms'; 
import { CustomValidators } from '../../components/CustomValidators'; 

/** 
* Generated class for the SignupPage page. 
* 
* See http://ionicframework.com/docs/components/#navigation for more info 
* on Ionic pages and navigation. 
*/ 


@IonicPage() 

@Component({ 
    selector: 'page-signup', 
    templateUrl: 'signup.html', 
}) 


export class SignupPage { 

    private sform : FormGroup; 

    constructor(
     private formBuilder: FormBuilder, 
     private myValidator: CustomValidators, 
    ){ 

    this.sform = this.formBuilder.group({ 
     name: ['', Validators.compose([Validators.maxLength(30), Validators.pattern('[a-zA-Z ]*'), Validators.required])], 
     email: ['', Validators.compose([Validators.email,this.myValidator.hasUniqueEmail])], 
     password: ['',Validators.required                        ], 
    }); 

    } 


    logForm() { 

    } 


} 

Đây là lỗi mà tôi nhận được:

"Uncaught (in promise): Error: No provider for CustomValidators! 
Error 
    at Error (native) 
    at injectionError (http://localhost:8100/build/main.js:1583:86) 
    at noProviderError (http://localhost:8100/build/main.js:1621:12) 
    at ReflectiveInjector_._throwOrNull (http://localhost:8100/build/main.js:3122:19) 
    at ReflectiveInjector_._getByKeyDefault (http://localhost:8100/build/main.js:3161:25) 
    at ReflectiveInjector_._getByKey (http://localhost:8100/build/main.js:3093:25) 
    at ReflectiveInjector_.get (http://localhost:8100/build/main.js:2962:21) 
    at NgModuleInjector.get (http://localhost:8100/build/main.js:3909:52) 
    at resolveDep (http://localhost:8100/build/main.js:11369:45) 
    at createClass (http://localhost:8100/build/main.js:11225:91)" 
+0

Là 'CustomValidators' một thành phần, một dịch vụ, một đường ống .. lớp được cho là đại diện là gì? – Baruch

Trả lời

14

Bạn cần phải thêm các nhà cung cấp cho NgModule , tức là module.ts trong nhà cung cấp,

providers: [ 
    CustomValidators 
] 
+0

Tôi đã thử nó trước đó nhưng có lỗi này !! "Lỗi không bắt buộc: Không thể giải quyết tất cả các tham số cho" Các bạn có biết bất kỳ hướng dẫn nào bao gồm tất cả các chủ đề này trong angular4 không? –

+0

thì bạn cần sử dụng @inject và sử dụng nó bên trong hàm tạo – Sajeetharan

0

Từ những gì tôi có thể thấy bạn đang thiếu 2 điều

1) không trang trí cho lớp, bạn đang nhập khẩu Directive nhưng không bao giờ sử dụng nó

import { Directive } from '@angular/core'; 

@Directive({ 
    name: 'directive-name' 
}) 
export class CustomValidators { 
    //... 
} 

2) không có nhập khẩu the NgModule

import { CustomValidators } from 'path/to/file'; 

@NgModule({ 
    //... 
    providers: [ 
    CustomValidators 
    ] 
}) 
Các vấn đề liên quan