您的位置:首页 > Web前端 > AngularJS

angular,使用directive实现,将input的数字保留两位小数,不足两位补0.

2017-09-04 14:16 645 查看
https://www.oschina.net/code/snippet_929096_53265

angular.module('app').directive('numFormat',function() {
return {
scope:{
model : '=ngModel'
},
link: function(scope, elm, attrs, ctrl) {

function format(){
if(isNaN(scope.model) || scope.model == ""){
scope.model = "0.00";
}else{
//最多保留两位小数
var f = parseFloat(scope.model);
var f = Math.round(scope.model*100)/100;
var s = f.toString();
var rs = s.indexOf('.');
if(rs < 0){
s = s + ".00";
}else{
while(s.length <= rs + 2){s += '0';}
}
scope.model = s;
}
}
format();//初始化
$(elm).bind("blur",format);//jq方式绑定事件
}
};
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐