我嘗試將值系結到 ValidationErrors 中。
我有這個方法:
isUniqueEmail(control: FormControl): ValidationErrors {
if (control.value != null) {
console.log(control.value)
if(control.value == this.foundEmail){
console.log("found one");
return {isUniqueEmail: true}
}else{
return null;
}
}
}
此方法檢查 control.value(電子郵件輸入)是否等于存盤在全域變數中的電子郵件,this.foundEmail然后我們有重復的電子郵件。
我的問題是:我可以用這個方法從 foundEmail 中檢索資料,因為這個方法是私有的。
此方法位于匯出類內部exampleComponent實作 OnInit。
錯誤: ERROR TypeError: Cannot read properties of undefined (reading 'foundEmail')
但我檢查我有資料 foundEmail
uj5u.com熱心網友回復:
驗證器函式是從 呼叫的,FormControl因此它的背景關系不會系結到您定義方法的類。您需要手動系結isUniqueEmail()到this.
兩種選擇:
在定義 FormControl 時使用bind():
name: new FormControl("", [ this.isUniqueEmail.bind(this), ]),將您的驗證器定義為箭頭函式:
isUniqueEmail = (control: FormControl) => {
if (control.value != null) {
console.log(control.value)
if(control.value == this.foundEmail){
console.log("found one");
return {isUniqueEmail: true}
}else{
return null;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/326939.html
標籤:javascript 有角的 验证
