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的