1
如何在textbox.please中輸入小數點後兩位小數,讓我知道如何修改下面的指令。在角度js中只輸入小數點後兩位小數點
我用下面的代碼
app.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 (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();
}
});
}
};
});