我需要从静态方法内部访问我的自定义http服务,例如:
import {Control} from 'angular2/common';
import {HttpService} from './http.service';
class UsernameValidator {
static usernameExist(control: Control): Promise<ValidationResult> {
... /* Access my HTTPservice here */
}
}
在这种情况下如何访问服务?
另一种方法是返回一个函数。这样,此函数可以访问HttpService
创建期间提供的实例:
class UsernameValidator {
static createUsernameExist(http:HttpService) {
return (control: Control) => {
... /* Access my HTTPservice here */
}
}
}
然后可以像这样使用它:
validator: UsernameValidator.createUsernameExist(this.httpService)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句