首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Angular.js:如何为价格添加用户输入并保持小计

Angular.js:如何为价格添加用户输入并保持小计
EN

Stack Overflow用户
提问于 2019-03-12 13:02:15
回答 1查看 55关注 0票数 0

全。我正在尝试制作一个购物清单,里面有一系列的食物,每种食物旁边都会提示用户价格和数量,然后保持所有商品的分类汇总,我有基本的,但真的很难找到“价格框”的数量和分类汇总。

代码语言:javascript
复制
var app = angular.module("myShoppingList", []);
app.controller("myCtrl", function($scope) {
  $scope.products = ["Milk", "Bread", "Cheese"];

  $scope.addItem = function() {
    $scope.errortext = "";
    if (!$scope.addMe) {
      return;
    }
    if ($scope.products.indexOf($scope.addMe) == -1) {
      $scope.products.push($scope.addMe);
    } else {
      $scope.errortext = "The item is already in your shopping list.";
    }
  }
  $scope.removeItem = function(x) {
    $scope.errortext = "";
    $scope.products.splice(x, 1);
  }
});
代码语言:javascript
复制
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">

<body>
  <div ng-app="myShoppingList" ng-cloak ng-controller="myCtrl" class="w3-card-2 w3-margin" style="max-width:400px;">
    <header class="w3-container w3-light-grey w3-padding-16">
      <h3>My Shopping List</h3>
    </header>
    <ul class="w3-ul">
      <li><input type="text" name="price" class="price" style="width: 50px" /></li>
      <li ng-repeat="x in products" class="w3-padding-16">{{x}}<span ng-click="removeItem($index)" style="cursor:pointer;" class="w3-right w3-margin-right">×</span></li>
    </ul>
    <div class="w3-container w3-light-grey w3-padding-16">
      <div class="w3-row w3-margin-top">
        <div class="w3-col s10">
          <input placeholder="Add shopping items here" ng-model="addMe" class="w3-input w3-border w3-padding">
        </div>
        <div class="w3-col s2">
          <button ng-click="addItem()" class="w3-btn w3-padding w3-green">Add</button>
        </div>
      </div>
      <p class="w3-text-red">{{errortext}}</p>
    </div>
  </div>
</body>
</html>

EN

回答 1

Stack Overflow用户

发布于 2019-03-12 14:21:22

确实有更好的方法来组织代码,但底线是你需要让你的products数组包含你需要的所有属性(名称,数量和价格)的对象,当你添加一个项目时,你只需将一个完整的新对象推送到数组中,而不仅仅是名称。在ng-repeat中有一个对象意味着您现在需要相应地访问需要在视图中显示的属性。请参阅代码片段

代码语言:javascript
复制
var app = angular.module("myShoppingList", []);
app.controller("myCtrl", function($scope) {
  $scope.products = [{
      name: "Milk",
      quantity: 1,
      price: 2.00
    },
    {
      name: "Bread",
      quantity: 2,
      price: 4.00
    },
    {
      name: "Cheese",
      quantity: 3,
      price: 6.00
    }
  ];

  $scope.quantity = 1;

  $scope.getTotal = function() {
    var total = 0;
    for (var i = 0; i < $scope.products.length; i++) {
      total += $scope.products[i].quantity * $scope.products[i].price;
    }
    return total;
  }

  $scope.addItem = function() {
    $scope.errortext = "";
    if (!$scope.addMe) {
      return;
    }
    if (true /*need a new way to check for duplicates */ ) {
      $scope.products.push({
        name: $scope.addMe,
        quantity: $scope.quantity || 0,
        price: $scope.price || 0
      });
    } else {
      $scope.errortext = "The item is already in your shopping list.";
    }
  }
  $scope.removeItem = function(x) {
    $scope.errortext = "";
    $scope.products.splice(x, 1);
  }
});
代码语言:javascript
复制
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">

<body>
  <div ng-app="myShoppingList" ng-cloak ng-controller="myCtrl" class="w3-card-2 w3-margin" style="max-width:400px;">
    <header class="w3-container w3-light-grey w3-padding-16">
      <h3>My Shopping List</h3>
    </header>
    <ul class="w3-ul">
      <li><input type="text" name="price" class="price" style="width: 50px" /></li>
      <li ng-repeat="x in products" class="w3-padding-16">{{x.name}} |
        <input style="width: 50px" type="number" ng-model="x.quantity" min="0"> | $
        <input style="width: 50px" type="number" ng-model="x.price" min="0"> | Subtotal ${{x.quantity * x.price}}<span ng-click="removeItem($index)" style="cursor:pointer;" class="w3-right w3-margin-right">×</span></li>
    </ul>
    Total ${{getTotal()}}
    <div class="w3-container w3-light-grey w3-padding-16">
      <div class="w3-row w3-margin-top">
        <div class="w3-col s10">
          <input placeholder="Add shopping items here" ng-model="addMe" class="w3-input w3-border w3-padding">
          <input placeholder="Quantity" ng-model="quantity" type="number" min="1">
          <input placeholder="Price" ng-model="price" type="number" min="1">
        </div>
        <div class="w3-col s2">
          <button ng-click="addItem()" class="w3-btn w3-padding w3-green">Add</button>
        </div>
      </div>
      <p class="w3-text-red">{{errortext}}</p>
    </div>
  </div>
</body>

</html>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55114482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档