重置Angular JS中的输入框的值

时间:2016-05-05 12:40:42

标签: javascript angularjs

我有以下简单的Angular应用程序。

HTML

Access-Control-Allow-Origin

controller.js文件:

https://use.fontawesome.com/

当我从一行中单击一个按钮时,该行的输入框中的值(即数量)应为0,并且列为"总价格"应修改为0.但这不会发生?我应该添加什么?

1 个答案:

答案 0 :(得分:2)

qty不是object的关键,将模型名称更改为item.qty,从数组访问时,请参阅index <的object / p>

注意:同时考虑[index]的拼写错误,您接受参数$index,但在访问密钥时,使用index

建议{/ strong> type='number' <input>更适合您的用例

&#13;
&#13;
var myApp = angular.module('myApp', []);

myApp.controller('MainController', function($scope) {
  $scope.title = 'Shopping Items';
  $scope.items = [{
    title: 'Item1',
    price: 3.95
  }, {
    title: 'Item2',
    price: 12.95
  }, {
    title: 'Item3',
    price: 6.95
  }];
  $scope.reset = function(index) {
    $scope.items[index].qty = "";
  }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>

<body ng-app='myApp' ng-controller="MainController">
  <h1><span class="label label-default">{{title}}</span></h1>
  <br/>
  <table class="table">
    <thead>
      <tr>
        <th>Item</th>
        <th>Unit price</th>
        <th>Quantity</th>
        <th>Total price</th>
      </tr>
    </thead>
    <tbody>
      <tr ng-repeat="item in items">
        <td>{{item.title}}</td>
        <td>{{item.price}}</td>
        <td>
          <input name="qty" type="number" ng-model="item.qty" />
        </td>
        <td>{{((item.price * item.qty) || 0) | number}}</td>
        <td>
          <button ng-click="reset($index)">Reset</button>
        </td>
      </tr>
    </tbody>
  </table>
</body>
&#13;
&#13;
&#13;

Fiddle Demo