Math functions in AngularJS bindings

后端 未结 13 1158
无人及你
无人及你 2020-11-29 17:55

Is there a way to use math functions in AngularJS bindings?

e.g.

The percentage is {{Math.round(100*count/total)}}%

13条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 18:45

    Why not wrap the whole math obj in a filter?

    var app = angular.module('fMathFilters',[]);
    
    
    function math() {
        return function(input,arg) {
            if(input) {
                return Math[arg](input);
            }
    
            return 0;
        }
    }
    
    return app.filter('math',[math]);
    

    and to use:

    {{ number_var | math:'ceil'}}

提交回复
热议问题