Math functions in AngularJS bindings

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

    That doesn't look like a very Angular way of doing it. I'm not entirely sure why it doesn't work, but you'd probably need to access the scope in order to use a function like that.

    My suggestion would be to create a filter. That's the Angular way.

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

    Then in your HTML do this:

    The percentage is {{ (100*count/total) | ceil }}%

提交回复
热议问题