Math functions in AngularJS bindings

后端 未结 13 1172
无人及你
无人及你 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:19

    The number filter formats the number with thousands separators, so it's not strictly a math function.

    Moreover, its decimal 'limiter' doesn't ceil any chopped decimals (as some other answers would lead you to believe), but rather rounding them.

    So for any math function you want, you can inject it (easier to mock than injecting the whole Math object) like so:

    myModule.filter('ceil', function () {
      return Math.ceil;
    });
    

    No need to wrap it in another function either.

提交回复
热议问题