Angular切换密码输入框类型时的报错问题及解决方案
Angular切换密码输入框类型的问题解决
- 需求:点击div触发函数,将两个
password类型的输入框切换为text类型 - 遇到的两类报错:
- 初始报错:
'password'可能为null - 添加非空断言后报错:
Property 'type' does not exist on type 'Element'
- 初始报错:
- 补充说明:该逻辑在纯HTML+JS环境可正常运行,但直接把原生JS代码搬到Angular中无效
解决方法
放弃直接修改type属性的写法:
password.type = "text"
改用setAttribute方法修改属性:
password.setAttribute("type", "text")
内容的提问来源于stack exchange,提问作者alias1
相关产品推荐
相关产品推荐

