angular - 如何检查 ngModel 输入字段是否脏?
问题描述
我有这个 HTML 模板:
<input type="text"
ngModel
#myValue="ngModel"
name="{{ fieldName }}"
id="{{ fieldName }}"
value="{{ myVal }}"
class="form-control"
(change)="checkDirty(myValue)">
如何检查 my.component.ts 文件中的此字段是否脏?
现在我在 my.component.ts 文件中只有这个基本代码:
export class UriFieldComponent implements OnInit {
constructor() { }
ngOnInit() { }
checkDirty(value) {
// in here I need to check is dirty or not
}
}
解决方案
NgModel 类中有一个脏属性。这意味着您可以执行以下操作:
checkDirty(value) {
if (value.dirty) {
...
}
}
但是,您使用它的方式不正确,如果您检查输入更改时的脏属性,很明显输入是脏的!换句话说,如果你要使用你的checkDirty
函数,输入总是很脏的。
推荐阅读
- python - 如何通过多列条件过滤时间序列数据框中的行
- stm32 - 用CAN总线对STM32微控制器编程的问题
- python - 使用漂亮的汤从标签中提取适当格式的文本(中间有空格)
- migration - 无法从旧的 3.4.2 keycloak 迁移到最新版本
- python - 如果您不小心重复了一个测试,Python unittest 不会注意到
- angular - 在异步 for 循环中设置 div 的背景图像
- python - 如何追溯警告
- android - 单击推送通知时无法获取有效负载反应本机
- html - 我有一个卷轴,但我希望它们彼此独立。有没有办法解决这个问题?
- javascript - 在音频标签的 src 属性中隐藏直接链接的可能方法?需要屏蔽链接的通用目录