Angularjs 角度输入指令不传递值
我正在尝试编写一个指令,在幕后将逗号转换为点,这样输入字段中的输入就可以正确地保存到数据库中。我仍然希望该字段显示未编辑的输入 我得到以下信息:Angularjs 角度输入指令不传递值,angularjs,directive,Angularjs,Directive,我正在尝试编写一个指令,在幕后将逗号转换为点,这样输入字段中的输入就可以正确地保存到数据库中。我仍然希望该字段显示未编辑的输入 我得到以下信息: App.directive('input', function () { return { restrict: 'E', require: 'ngModel', scope: { }, link: function(scope, iElement, iAttrs, ngModelController) {
App.directive('input', function () {
return {
restrict: 'E',
require: 'ngModel',
scope: {
},
link: function(scope, iElement, iAttrs, ngModelController) {
if (iAttrs.type !== 'commasallowed') return;
iElement.unbind('input');
iElement.bind('input', function() {
ngModelController.$render = function() {
console.log("start render");
iElement.val(scope.screenValue);
};
scope.$apply(function () {
scope.screenValue = iElement.val();
scope.toDb = parseFloat(scope.screenValue.toString().replace(",", "."));
ngModelController.$setViewValue(scope.toDb);
console.log(ngModelController.$viewValue);
ngModelController.$render();
});
});
}
};
});
当我开始在输入字段中键入时,第25行中的console.log会记录ngModelController的正确值。$viewValue正确记录为带点的数字,而不是逗号,
但是,当我处理表单时,只有保存点之前的数字……可能使用angular的格式化程序和解析器服务可以帮助您。这是一种更具角度感的方式。谢谢你的帮助,我对角度感很陌生,不知道这些功能。所以我不需要自己写这些乱七八糟的东西,现在这是一个非常简单的指令。