从angularjs

时间:2016-04-23 11:30:51

标签: html angularjs

在此角度js app中,我无法正确应用用户在计算总价之前选择的小数舍入。

HTML

<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="lolCtrl">
  <form class = "col-md-6">
  <div class = "form-group">
    <label for = "prezzo"> Prezzo </label>
    <input type="number" class = "form-control" placeholder = "Prezzo" ng-model="price">
  </div>
  <div class = "form-group">
    <label for = "percentuale"> Percentuale </label>
    <input type="number" class = "form-control" placeholder = "Percentuale" ng-model="percent">
  </div>
  <div class = "form-group">
     <label for = "precisione"> n° decimali </label>
     <select class = "form-control" ng-model = "selectPrecision" ng-options="num.value as num.precision for num in decimali"></select>
  </div>
</form>
<button class = "btn btn-default" ng-click="percentuale()">Prezzo</button>
<p>Il risultato è:<span ng-model = "selectPrecision">{{risultato | number : 7}}</span></p>
  </div>
</body>

JS

var app = angular.module('myApp', []);

app.controller('lolCtrl', function($scope){
$scope.price = 20.232223;
$scope.percent = 20;

$scope.decimali= [
{value:0, precision: "0 decimali"},
{value:1, precision: "1 decimali"},
{value:2, precision: "2 decimali"}
];

$scope.percentuale = function (){
   $scope.risultato = $scope.price + $scope.price* $scope.percent/100;

}

});

我在这个例子中摆弄了很多,因为我不想创建一个自定义过滤器,但它让我无法接近我想要的结果:

Plunkr

为了给你工作代码,我给了一个固定数量的7位小数。我希望它没有自定义过滤器是动态的,因为我只需要将参数传递给已经存在的框架过滤器函数。

它是如何完成的?

1 个答案:

答案 0 :(得分:0)

只需更改

即可
<p>Il risultato è:<span ng-model = "selectPrecision">{{risultato | number : 7}}</span></p>

<p>Il risultato è:<span ng-model = "selectPrecision">{{risultato | number : selectPrecision}}</span></p>