Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 使用NgModel对象引用更改输入值。角度5_Angular - Fatal编程技术网

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 did
this.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;
}