Math functions in AngularJS bindings

后端 未结 13 1127
无人及你
无人及你 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.

    0 讨论(0)
  • 2020-11-29 18:20

    I think the best way to do it is by creating a filter, like this:

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

    then the markup looks like this:

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

    Updated fiddle: http://jsfiddle.net/BB4T4/

    0 讨论(0)
  • 2020-11-29 18:21

    Angular Typescript example using a pipe.

    math.pipe.ts

    import { Pipe, PipeTransform } from '@angular/core';
    
    @Pipe({
      name: 'math',
    })
    
    export class MathPipe implements PipeTransform {
    
      transform(value: number, args: any = null):any {
          if(value) {
            return Math[args](value);
          }
          return 0;
      }
    }
    

    Add to @NgModule declarations

    @NgModule({
      declarations: [
        MathPipe,
    

    then use in your template like so:

    {{(100*count/total) | math:'round'}}
    
    0 讨论(0)
  • 2020-11-29 18:24

    The easiest way to do simple math with Angular is directly in the HTML markup for individual bindings as needed, assuming you don't need to do mass calculations on your page. Here's an example:

    {{(data.input/data.input2)| number}} 
    

    In this case you just do the math in the () and then use a filter | to get a number answer. Here's more info on formatting Angular numbers as text:

    https://docs.angularjs.org/api/ng/filter

    0 讨论(0)
  • 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:

    <p>The percentage is {{ (100*count/total) | ceil }}%</p>
    
    0 讨论(0)
  • 2020-11-29 18:26

    While the accepted answer is right that you can inject Math to use it in angular, for this particular problem, the more conventional/angular way is the number filter:

    <p>The percentage is {{(100*count/total)| number:0}}%</p>
    

    You can read more about the number filter here: http://docs.angularjs.org/api/ng/filter/number

    0 讨论(0)
提交回复
热议问题