I previously write this [question][1], Inow I have the problem that the model.$viewValue it's not the same of the value the i see in the input box.
<div amount-input-currency="" ng-model="data.amount" ></div>
This is my directive (isNumeric and similar is not important that works weel):
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.data = { amount: ''};
});
app.directive('amountInputCurrency', function () {
return {
restrict: 'EA',
require: 'ngModel',
templateUrl: 'inputCurrency.tmpl.html',
scope: {
model: '=ngModel',
},
link: function (scope, elem, attrs, ngModelCtrl) {
scope.model2 = ngModelCtrl;
console.log("I am in the directive!");
var myAmountCurrencyType = elem.find('.cb-amount-input-currency');
scope.onFocus = function() {
removeThousandSeparator();
};
scope.onBlur = function() {
renderValue();
ngModelCtrl.$render();
};
//format text going to user (model to view)
ngModelCtrl.$formatters.push(function(value) {
return parseValue(value);
});
//format text from the user (view to model)
ngModelCtrl.$parsers.push(function(value) {
var num = Number(value);
if(isNumeric(num)) {
var decimal = 2;
return formatAmount();
} else {
return value;
}
});
function isNumeric(val) {
return Number(parseFloat(val))==val;
}
}
}
});
And this is my template:
scope.model: {{model}}<br>
viewValue: {{model2.$viewValue}}<br>
modelValue: {{model2.$modelValue}}<br>
<input type="text" class="amount-input-currency form-control" x-ng-model="model" ng-focus="onFocus()" ng-blur="onBlur()"></input>
Set the viewValue using
ngModelCtrl.$setViewValue()
in order to update the model instead of setting $viewValue field directly. But I am not sure what is the point of using NgModelController in this case at all.If the only purpose is to format the value of the textbox, manipulate the input element value instead of NgModelController fields.
This way it does not update the model. If you want to have full control over the data binding you can consider removing the binding from the input element
x-ng-model="model"
and implementing it using NgModelController in your directive.Checkout Formatters and Parser, it's how to do what you want, but you have to require ngModel to hook append formatter or parser.
A good article about them : https://alexperry.io/angularjs/2014/12/10/parsers-and-formatters-angular.html
Regards.
If you can do what you need to do without formatters and parsers it's better because that you work more in the angular way, and if you can avoid requiring ng-model in directives if you can manage without it because that cause a lot of mess too.
As for your problem I will show a solution that does not need the formatters and parsers and I hope that this what you wanted, if you must use the formatters and parsers you can, but it will essentially do the same as the following solution:
index.html:
amountInputCurrency.tmpl.html:
app.js:
If this is not what you want then I'm truly sorry and please comment what is the problem in my solution for and I will try my best to see if I can help.