Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.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
Angular2:如何修改表单控件';在放入数据模型之前输入值_Angular - Fatal编程技术网

Angular2:如何修改表单控件';在放入数据模型之前输入值

Angular2:如何修改表单控件';在放入数据模型之前输入值,angular,Angular,我有一个表单模型: form=newformgroup({currency:newformcontrol('')}) 在控件中输入值时启用了掩蔽字符: 123456美元 现在,如何使我的form.value返回{currency:123456} 我尝试了this.form.controls['currency'].setValue(123456),但没有成功。您可以使用指令ngModel。$parsers.push和ngModel。$formatters.push 'use strict' //

我有一个表单模型:

form=newformgroup({currency:newformcontrol('')})

在控件中输入值时启用了掩蔽字符:

123456美元

现在,如何使我的
form.value
返回
{currency:123456}


我尝试了
this.form.controls['currency'].setValue(123456)
,但没有成功。

您可以使用指令ngModel。$parsers.push和ngModel。$formatters.push

'use strict'
// Directive to show the browser sepcific decimal separator for Numeric 
controls.
angularPracticeApp.directive('apNumberInput', function () {
return {
    restrict: 'A',
    require: '^ngModel',
    scope: {},
    link: function (scope, element, attr, ngModel) {

        // Runs when the value change from UI.
        ngModel.$parsers.push(function (value) {
            if (value && typeof (value) === "string")
                return value.replace(',', '.');
            else
                return value;
        });

        // Runs when the value change from code.
        ngModel.$formatters.push(function (value) {
            if (value && typeof (value) === "string")
                return value.replace('.', ',');
            else
                return value;
        });
    }
}
});

你如何掩盖这个值?因为一旦你使用setValue,这个值就会被再次屏蔽,对吗?我正在使用这个库。它与
ngModel
上的
controlValueAccessor
一起工作-我删除了
ngModelChange
中的掩码字符。但是不知道如何用反应式表单实现同样的效果。你也可以用反应式表单使用
ngModelChange
。我们找到了这个。form.valueChanges.subscribe(obj=>obj.field=obj.field.replace(/\D+/g');滚动到底部我想问题是关于angular2的