3
我需要實現與github api的異步驗證。我希望你能幫助我。Angular2用戶名或電子郵件採取異步驗證
export class UsernameValidator {
static usernameTaken(control: FormControl): Promise<ValidationResult> {
return new Promise((resolve, reject) => {
setTimeout(() => {
//How Can i use github Api like this:
// if (control.value === "this._http.get('http://api.github.com/users/'+this.username)")) {
if (control.value === "David") {
console.log('username taken')
resolve({"usernameTaken": true})
} else {
resolve(null);
};
}, 1000);
});
}
}
謝謝。
你讓我很快樂!謝謝。它的作品非常酷 –
@AlexanderBorovoi請檢查我的問題我有一個非常類似的情況http://stackoverflow.com/questions/40756441/custom-async-validation-not-working-when-returning-a-promise – Luther
可以你請在你的例子中添加一個去抖時間?我不希望每個按鍵發送http請求。 – Michalis