32

有没有办法在 angular2 绑定中使用数学函数?

例子

<div class="partition-panel">
                <b class="pull-left">{{Math.round(variable/12*2)}}</b>
                <b class="pull-right">{{Math.round(variable/12*2)}}</b>
 </div>

当尝试使用它时我得到了错误

Cannot read property 'round' of undefined

angular1也回答了类似的问题

4

2 回答 2

79

你可以试试这个:

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{math.round(number)}}</h2>
    </div>
  `,
})
export class App {
  number = 2.5;
  math = Math;
}

演示

于 2016-11-16T12:58:41.117 回答
12

对于 Angular 模板中的舍入数字,您可以使用 DecimalPipe:{{ value | number }}

查看https://angular.io/api/common/DecimalPipe中的所有舍入选项

对于所有内置管道,请检查 https://angular.io/api?type=pipe

于 2018-10-29T11:30:31.403 回答