Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.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/9/google-cloud-platform/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
Javascript 在输入上定义表单控件时,NgModel.viewToModelUpdate不起作用_Javascript_Angular_Angular Ngmodel_Angular Directive_Ngmodel - Fatal编程技术网

Javascript 在输入上定义表单控件时,NgModel.viewToModelUpdate不起作用

Javascript 在输入上定义表单控件时,NgModel.viewToModelUpdate不起作用,javascript,angular,angular-ngmodel,angular-directive,ngmodel,Javascript,Angular,Angular Ngmodel,Angular Directive,Ngmodel,我想通过一个指令更改带有onBlur事件的输入上的视图和模型数据 我编写此函数是为了更新视图和模型: @HostListener('blur',['$event']) blur(e:Event){ const target:HTMLInputElement = <HTMLInputElement>e.target; const val = this.decimalPipe.transform(target.value.replace(/\,/g,''),'1.2-

我想通过一个指令更改带有onBlur事件的输入上的视图和模型数据

我编写此函数是为了更新视图和模型:

@HostListener('blur',['$event'])
  blur(e:Event){
    const target:HTMLInputElement = <HTMLInputElement>e.target;
    const val = this.decimalPipe.transform(target.value.replace(/\,/g,''),'1.2-2');
    this.ngModel.viewToModelUpdate(Number(val.replace(/\,/g,'')));
    this.ngModel.valueAccessor.writeValue(val);
  }
它可以忽略此函数并将模型设置为字符串。如何将型号更新为编号?

尝试使用:

this.ngModel.control.patchValue(Number(val.replace(/\,/g,'')));
而不是

this.ngModel.viewToModelUpdate(Number(val.replace(/\,/g,'')));

Angular 5上formControl输入的自定义指令也存在相同问题。

是否使用viewChild获取ngModel属性?作为补充,您可以使用
this.ngModel.valueAccessor.writeValue来显示不同的视图值。
this.ngModel.viewToModelUpdate(Number(val.replace(/\,/g,'')));