You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular切换密码输入框类型时的报错问题及解决方案

Angular切换密码输入框类型的问题解决
  • 需求:点击div触发函数,将两个password类型的输入框切换为text类型
  • 遇到的两类报错:
    1. 初始报错:'password'可能为null
    2. 添加非空断言后报错:Property 'type' does not exist on type 'Element'
  • 补充说明:该逻辑在纯HTML+JS环境可正常运行,但直接把原生JS代码搬到Angular中无效

解决方法

放弃直接修改type属性的写法:

password.type = "text"

改用setAttribute方法修改属性:

password.setAttribute("type", "text")

内容的提问来源于stack exchange,提问作者alias1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 01:55:16