Angular 使用NgModel对象引用更改输入值。角度5
我有一个带有模板驱动验证的角度表单 例如: 现在,它将打印输入值(如果有)。 但是,我需要重置输入值,如果存在任何特定值。例如:Angular 使用NgModel对象引用更改输入值。角度5,angular,Angular,我有一个带有模板驱动验证的角度表单 例如: 现在,它将打印输入值(如果有)。 但是,我需要重置输入值,如果存在任何特定值。例如: ngOnInit() { if(this.un.value == "username") { // Reset the input value to null } } 但是,我不知道该怎么做 我试过: if(this.un.value == "username") { this.un.value = null; // <-- This
ngOnInit() {
if(this.un.value == "username") {
// Reset the input value to null
}
}
但是,我不知道该怎么做
我试过:
if(this.un.value == "username") {
this.un.value = null; // <-- This does nothing
}
if(this.un.value==“用户名”){
this.un.value=null;//尝试
un
应该是FormControl
,您应该尝试this.un.setValue(“xyz”)
何时需要重置值。为什么需要访问NgModel或DOM元素才能重置值?您使用的是双向绑定,所以只需执行this.loginDetail.username='
@BillCheng I didthis.un.control.setValue(null)
成功了。谢谢!!!@JBNizet我完全同意,但我有一个无法做到的情况。抱歉,无法解释原因。nativeElement
可用于模板引用变量。但是,this.un
是NgModel对象引用
ngOnInit() {
if(this.un.value == "username") {
// Reset the input value to null
}
}
if(this.un.value == "username") {
this.un.value = null; // <-- This does nothing
}
if(this.un.value == "username") {
this.un.nativeElement.value = null;
}