无法读取未定义的属性(读取"substr")



嗨,我正试图从基于一些计算的字符串中获得子字符串,但在浏览器控制台获得此错误:


TypeError: Cannot read properties of undefined (reading 'substring')
at MessageVariantCardInputComponent.onInput 
at MessageVariantCardInputComponent_input_HostBindingHandler

当使用下面的代码示例获取"value">

的子字符串值时代码:



@HostListener('input') onInput() {  
this.triggerFound = false;
this.triggerName = '';
const native = this.element.nativeElement;
console.log("=== native dropdown", native)
const value = native.value;
const currentPos = native.selectionStart - 1;

for (const key of Object.keys(this.triggers)) {
const trigger = this.triggers[key];
const triggerLen = trigger.length;
const extractedString = value.substr(currentPos - triggerLen + 1, triggerLen);
const found = extractedString === trigger;
const isFirstTrigger = key === 'firstElement';

if ((isFirstTrigger && found && currentPos === 0) || // allow first element trigger only when current pos is 0
(!isFirstTrigger && found)
) {
this.triggerFound = true;
this.triggerName = trigger;
break;
}
}
}

console.log

console.log("=== native dropdown", native) ?

最新更新