숫자 만 받고 추가 옵션이있는 지시문을 다운로드했습니다.
<input type="text" ng-model="mynumber" nks-only-number allow-decimal="false" />
내가 거짓 조건이 오류를 표시하고있다 생각하지만, 나는 이유를 알고하지 않습니다하지만, 그것을 실행 한 후 나는있는 옵션 중 하나에 rootScope의 오류가 발생합니다. 여기
데모입니다 :
http://jsfiddle.net/RmDuw/896/
코드 :
(function(){
angular.module('myApp', [])
.directive('nksOnlyNumber', function() {
return {
restrict: 'EA',
require: 'ngModel',
link: function (scope, element, attrs, ngModel) {
scope.$watch(attrs.ngModel, function(newValue, oldValue) {
var spiltArray = String(newValue).split("");
if(attrs.allowNegative == "false") {
if(spiltArray[0] == '-') {
newValue = newValue.replace("-", "");
ngModel.$setViewValue(newValue);
ngModel.$render();
}
}
if(attrs.allowDecimal == "false") {
newValue = parseInt(newValue);
ngModel.$setViewValue(newValue);
ngModel.$render();
}
if(attrs.allowDecimal != "false") {
if(attrs.decimalUpto) {
var n = String(newValue).split(".");
if(n[1]) {
var n2 = n[1].slice(0, attrs.decimalUpto);
newValue = [n[0], n2].join(".");
ngModel.$setViewValue(newValue);
ngModel.$render();
}
}
}
if (spiltArray.length === 0) return;
if (spiltArray.length === 1 && (spiltArray[0] == '-' || spiltArray[0] === '.')) return;
if (spiltArray.length === 2 && newValue === '-.') return;
/*Check it is number or not.*/
if (isNaN(newValue)) {
ngModel.$setViewValue(oldValue || '');
ngModel.$render();
}
});
}
};
});
}());
JSFiddle은 여기에 게시 된 코드와 유사하지 않습니다 ... 맞습니까? – Sam
동일합니다, 그냥 코드 directlly – Pedro