angular - 如何在Angular 7中处理修剪()和toLowerCase()的Null检查
问题描述
我在 Angular 7 中有一个输入字段,我使用 trim () 和 toLowerCase() 逻辑有效,但有时在控制台中显示 .trim() 和 toLowerCase() 未定义如何处理此错误请帮助。
<input
oninput="this.value = this.value.toLowerCase()"
(blur)="supplierInfo.name = supplierInfo.name.trim()"
[(ngModel)]="supplierInfo.name"
/>
解决方案
您可以使用安全导航运算符 ( ? ) 进行空/未定义检查
例如:
(blur)="supplierInfo?.name = supplierInfo?.name?.trim()"
为 this.value 添加相同的内容
oninput="this.value = this.value?.toLowerCase()" - In this case, it the new value is null/undefined - then it will retain the same
oninput="this.value = this.value?.toLowerCase() || ''" - Will update the string to a blank string
推荐阅读
- python - 如何根据熊猫中的公式从两个数据框中获取所有可能的组合?
- git - Git checkout branchname 创建不在 git log 中的文件
- aws-lambda - 通过 Lambda 查询 QLDB
- r - 如何在 R 中手动构建订单函数?
- android - ArCore ArFrame 隐藏相机图像但保留 HandMotion 动画
- android - 如何实现应用程序范围的搜索栏?
- algorithm - 将消息概率转发到服务器子集
- python - 尝试在 python 中使用 ioctl 读取驱动器属性时出现溢出错误
- javascript - 同时捕获文本和文本区域字段
- sql - SQL query to extract words from a text field