【发布时间】:2014-01-21 10:53:16
【问题描述】:
如果用户输入不是数字,我必须恢复为旧数字值。
从指令设置范围值不起作用。
http://jsfiddle.net/vfsHX/149/
app.directive('isNumber', function () {
return {
require: 'ngModel',
link: function (scope, element, attrs) {
scope.$watch(attrs.ngModel, function(newValue,oldValue) {
var arr = String(newValue).split("");
if (arr.length === 0) return;
if (arr.length === 1 && (arr[0] == '-' || arr[0] === '.' )) return;
if (arr.length === 2 && newValue === '-.') return;
if (isNaN(newValue)) {
console.log(oldValue);
scope[attrs.ngModel] = oldValue;
}
});
}
};
});
【问题讨论】:
标签: angularjs angularjs-directive angularjs-scope