typescript - Using an injected angular service as a dependency within a static method -
i trying use injected dependency within static method , of course injected dependency instance-scoped , can't used within static method.
here class:
@injectable() export class passwordvalidationservice { constructor(private useraccountservice:useraccountservice) { } static passwordvalidator(control:abstractcontrol) { return control .valuechanges .debouncetime(400) .switchmap(()=> this.useraccountservice.checkcurrentpassword(control.value)) .map(res=> { if (res.json() === true) { return null; } else { return {invalid: true}; } } ); } }
my question best practice in order use useraccountservice
(dependency) within static method?
edit: redesigned app towards using instance methods instead of static methods follows:
here validator:
import {injectable} "@angular/core"; import {useraccountservice} "../../useraccount/useraccount.service"; import {abstractcontrol} "@angular/common"; import {observable} "rxjs/observable"; @injectable() export class passwordvalidationservice { constructor(private useraccountservice:useraccountservice) { } passwordvalidator(control:abstractcontrol):observable<any> { let validationresult = this.useraccountservice.checkcurrentpassword(control.value) .map(res=> { if (res.json() === true) { return null; } else { return {invalid: true}; } } ); return validationresult; } }
here component using validator:
constructor(private router:router, private formbuilder:formbuilder, private stylingservice:stylingservice, private sessionservice:sessionservice, private passwordvalidationservice:passwordvalidationservice) { } ngoninit() { this.signinform = this.formbuilder.group({ credentials: this.formbuilder.group({ username: [this.credentials.username, validators.required], password: [this.credentials.password, [validators.required, this.passwordvalidationservice.passwordvalidator]] }) }); }
here error message get:
browser_adapter.ts:82 typeerror: cannot read property 'useraccountservice' of undefined @ passwordvalidationservice.passwordvalidator (http://localhost:8080/app/shared/services/password-validation.service.js:18:36) @ eval (http://localhost:8080/vendor/@angular/forms/src/validators.js:137:49) @ array.map (native) @ _executevalidators (http://localhost:8080/vendor/@angular/forms/src/validators.js:137:23) @ formcontrol.eval [as validator] (http://localhost:8080/vendor/@angular/forms/src/validators.js:116:33) @ formcontrol.abstractcontrol._runvalidator (http://localhost:8080/vendor/@angular/forms/src/model.js:178:56) @ formcontrol.abstractcontrol.updatevalueandvalidity (http://localhost:8080/vendor/@angular/forms/src/model.js:164:29) @ new formcontrol (http://localhost:8080/vendor/@angular/forms/src/model.js:304:14) @ formbuilder.control (http://localhost:8080/vendor/@angular/forms/src/form_builder.js:36:16) @ formbuilder._createcontrol (http://localhost:8080/vendor/@angular/forms/src/form_builder.js:68:25)
either don't make method static or forward instance method
export class passwordvalidationservice { constructor(private useraccountservice:useraccountservice) { } validate(control:abstractcontrol) { return passwordvalidationservice.passwordvalidator(control); } static passwordvalidator(control:abstractcontrol) { ... } }
Comments
Post a Comment