AngularJs directive使用自定义filter

/**
         * money formatter, will add <small> tag for the yuan and the decimal
         * <div ng-bind-html="number|slMoney [:shrink [:noHtml :decimalLength]]"></div>
         * */
        sl.filter('slMoney',['$filter','$sce',function($filter,$sce) {
            /**
             * @number input number , by default (mostly used) : 32123.34 --> 32,123<small>.34元</small>
             * @shrink true -- will put wy at the end ,
             *                should use this only when the number is multiple of 100,
             *                or with decimal 2 will lose the money in position 1 and 2
             *                slMoney(11230,true)--> 1<small>.12万元</small> , 30 will lost
             * @noHtml true -- won't add the <small> tag
             * @decimalLength define the decimal length, default 2 , usually we use 2
             * @noCurrencySign wont add yuan at the end
             */
            return function(number,shrink,noHtml,decimalLength,noCurrencySign) {
                number = replaceMoneySign(number);
                var _nf = $filter('number');
                var n = '', sign = y;
                var dl = decimalLength || decimalLength == 0 ? decimalLength : 2;
                if(!shrink) {
                    n = _nf(number, dl);
                }
                else{
                    var multiple = 1,m=1e4;
                    if(number >= m) { multiple = m;   sign = wy; }
                    n = _nf(number / multiple, number % multiple == 0 ? 0 : dl);
                }
                var index =  n.indexOf('.');
                var beforeDecimal = index < 0 ? n : n.substring(0, index);
                var afterDecimal = index < 0 ? '' : n.substring(index);
                sign = noCurrencySign  ? '' : sign;
                var res = beforeDecimal + (!noHtml ? '<small>':'') + afterDecimal + sign + (!noHtml ? '</small>':'');
                return $sce.trustAsHtml(res);
            };
        }]);

 

// js使用
sl.directive('slPlanCard', ['$sce', '$filter',
function($sce, $filter){
  amountObj.innerHTML = $filter('slMoney')(amount, true, true);
}

 

<!-- html使用 -->
 <p>{{ plan.minInvestAmount | slMoney:true:true }}</p>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值