This function works fine but when user enter first digit to text field it pass to the controller as a empty value and after second input it passes first input to the controller. I want to take value to controller on first input with correct value instead of empty value.
I have created custom directive for number validation
app.directive('faxFormat', ['$filter', function($filter) {
function link(scope, element, attributes) {
// scope.inputValue is the value of input element used in template
scope.inputValue = scope.phonenumberModel;
scope.$watch('inputValue', function(value, oldValue) {
value = String(value);
var number = value.replace(/[^0-9]+/g, '');
// scope.phonenumberModel = number;
scope.inputValue = $filter('phonenumber')(number);
scope.phonenumberModel = scope.inputValue;
});
scope.$watch('phonenumberModel', function(value, oldValue) {
scope.inputValue = scope.phonenumberModel;
});
// element.bind('click', function(){
// scope.method();
// });
scope.phoneFocused = function(){
scope.method();
}
}
return {
link: link,
//restrict: 'A',
//require: 'ngModel'
restrict: 'E',
scope: {
method: '&',
phonenumberPlaceholder: '=placeholder',
phonenumberModel: '=model',
myid: '=myid'
},
template: '<input ng-model="inputValue" type="tel" id="{{myid}}" class="phonenumber {{cls}}" ng-trim="false" placeholder="{{phonenumberPlaceholder}}" title="Phonenumber (Format:999-999-9999 or 1-999-999-9999)" ng-change="phoneFocused()">'
};
}]);
Here is my controller
$scope.removePhoneErrorHighlight = function() {
var fax =$scope.phoneData.phoneNumber;
console.log(fax);
}
Here is my html code
<fax-format class="faxFormat" myid="'accountPhone'" placeholder="'555-555-5555'" model="phoneData.phoneNumber" method="removePhoneErrorHighlight()" data-ng-focus="removePhoneErrorHighlight()"></fax-format>