angularjs實現自動計算商品總價格程式碼例項

antzone發表於2017-04-13

本章節分享一段程式碼例項,它實現了自動計算商品總價格的功能。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<script src="http://code.angularjs.org/angular-1.0.1.min.js"></script>
</head>
<body>
<div ng-app="myApp">
  <div ng-controller="myController">
    單價:<input type="text" ng-model="pen.money" /><br />
    數量:<input type="text" ng-model="pen.number" /><br />
    商品總價:{{proPrice() | currency : '¥'}}<br />
    運輸費用:{{pen.trans | currency : '¥'}}<br />
    總額:{{proPrice()+pen.trans | currency : '¥'}}
  </div>
</div>
<script>
var app = angular.module("myApp", []);
app.controller('myController', function ($scope) {
  $scope.pen={
    money:1.5,
    number:1,
    trans:8
  }
  $scope.proPrice = function(){
    return $scope.pen.money*$scope.pen.number;
  }
  $scope.$watch($scope.proPrice,function(newVal,oldVal){
    $scope.pen.trans = newVal>=5?0:8;
  })
})
</script>
</body>
</html>

相關文章