AngularJS购物车商品数量加减价格实时变化代码
发布时间:2016-03-25, 12:02:03 分类:HTML | 编辑 off 网址 | 辅助
正文 1207字数 479,457阅读
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="personCtrl" data-ng-app="" data-ng-init="quantity=1;price=5">
<h2>价格计算器</h2>
数量:<button ng-click="toggle();" >-</button> <input type="number" ng-change="toggles()" ng-model="quantity"><button ng-click="quantity = quantity + 1">加</button>
价格: <input type="number" ng-model="price">
<p><b>总价:</b> {{quantity * price}}</p>
</div><script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
$scope.toggle = function() {
if($scope.quantity>1) $scope.quantity =$scope.quantity - 1;
};
$scope.toggles = function() {
if($scope.quantity<0 || isNaN($scope.quantity)) $scope.quantity =1;
};
});
</script>
</body>
</html>
Run code
Cut to clipboard
(支付宝)给作者钱财以资鼓励 (微信)→
有过 1 条评论 »
使用时候
if(!isNaN(val)){ alert("是数字"); }else{ alert("不是数字"); }