AngularJs에 문자 구분자가있는 입력란에 입력 된 번호의 서식을 지정하고 싶습니다.ngModel이있는 사용자 정의 문자 구분 기호가 올바르게 작동하지 않습니다.
서식을 지정하라는 지시문을 만들었습니다. 지시문이 올바른 출력을 생성하고 있지만 ngModel
을 올바르게 업데이트 할 수 없습니다.
<input type="text" data-ng-model="aadhaar" dashsep="'-'">
.directive('dashsep', function($timeout) {
'use strict';
return {
restrict: 'A',
scope: {
dashsep: '=',
ngModel: '=ngModel'
},
require: '?ngModel',
link: function(scope, element, attr) {
element.bind("keyup", function (event) {
var formattedAdh = "";
//console.log(element.val());
var test = element.val().toString();
for(var i=0;i<=test.length-1;i++){
//console.log(test[i]);
if(i%4 === 0 && i>0){
formattedAdh+= scope.dashsep;
}
formattedAdh += test[i];
}
// Following script is not updateing the ngModel/textbox with the formatted value properly //
/*
//element.val(scope.$eval(formattedAdh));
// OR
//scope.ngModel = formattedAdh;
*/
// Following output is showing proper value IF ABOVE SCRIPT IS COMMENTED //
console.clear();
console.log(formattedAdh);
});
}
};
});
전체 스크립트가 Plunker에 제공됩니다.