2017-06-16 14 views
6

में त्रुटि "के लिए कोई प्रदाता नहीं है Ionic 3 सीख रहा हूं और मुझे एक कस्टम सत्यापनकर्ता बनाने की कोशिश करते समय यह त्रुटि मिल रही है जो अद्वितीय के लिए जांचता है उपयोगकर्ता नाम। मैंने अपनी पूरी कोशिश की है लेकिन इस मुद्दे को हल नहीं कर सका। आप NgModule करने के लिए प्रदाता जोड़ने की जरूरत हैकैसे हल करें "त्रुटि: अनकॉल्ड (वादे में): त्रुटि:" Ionic 3

"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

है 'एक घटक है, एक सेवा, एक पाइप .. क्या वर्ग का प्रतिनिधित्व करने के लिए माना जाता है CustomValidators'? – Baruch

उत्तर

14

:

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() { 

    } 


} 

यह त्रुटि है कि मैं हो रही है प्रदाताओं के तहत, यानी module.ts,

providers: [ 
    CustomValidators 
] 
+0

मैंने इसे पहले कोशिश की है लेकिन यह त्रुटि मिली है !! "Uncaught त्रुटि: के लिए सभी मापदंडों को हल नहीं कर सकते" तुम लोग किसी भी ट्यूटोरियल है कि angular4 में इन सभी विषयों को शामिल पता है? –

+0

तो आपको @inject का उपयोग करने और कन्स्ट्रक्टर के अंदर इसका उपयोग करने की आवश्यकता है – Sajeetharan

0

मैं क्या देख सकते हैं आप 2 बातें

1) वर्ग के लिए कोई डेकोरेटर भूल रहे हैं से, आप Directive आयात कर रहे हैं, लेकिन यह कभी नहीं का उपयोग कर

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

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

2) वहाँ में कोई आयात है NgModule

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

@NgModule({ 
    //... 
    providers: [ 
    CustomValidators 
    ] 
}) 
संबंधित मुद्दे