用artTemplate模板設置自定義函數以即自定義函數使用方法
自定義函數
// 百分比計算 template.defaults.imports.percentage = function (num1, num2) {if(!num1 && !num1){ return 0+'%'; }else{ return Math.round(num1 / (num1 + num2) * 100) + '%'; } }
自定義函數使用
<script type="text/html" id="content">
<div class="coping-ratio-content-fl">
<div class="coping-ratio-content-title">
百分比<span>{{cash,total | percentage}}</span>
</div>
</div>
</script>
數據填充
$(".header").html(template('content',res.data));