如何在离子列表中添加/删除项目

时间:2015-08-25 21:36:23

标签: angularjs cordova ionic-framework ionic angularjs-model

我在VS2015中创建了选项卡式离子应用程序。现在我想添加一个简单的列表,可能添加/删除项目(类似于此内容 - sample of angularjs app

我的HTML代码(tab-chats.html):

<ion-view view-title="Chats">
  <ion-content>
    <div id="AddItem">
        <h3>Add Item</h3>
        <input value="1" type="number" placeholder="1" ng-model="itemAmount">
        <input value="" type="text" placeholder="Name of Item" ng-model="itemName">
        <br />
        <button ng-click="addItem()">Add to list</button>
    </div>
    <div id="UncheckedList">
        <h4>Unchecked:</h4>
        <table>
            <tr ng-repeat="item in items" class="item-unchecked">
                <td><b>amount:</b> {{item.amount}} -</td>
                <td><b>name:</b> {{item.name}} -</td>
                <td>
                    <button ng-click="removeItem($index)">remove</button>
                </td>
            </tr>
        </table>
    </div>
  </ion-content>
</ion-view>

我在controllers.js中的JavaScript代码:

.controller('ChatsCtrl', function ($scope) {
    $scope.items = [];
    $scope.addItem = function () {
        $scope.items.push({
            amount: $scope.itemAmount,
            name: $scope.itemName
        });

        $scope.itemAmount = "";
        $scope.itemName = "";
    };
    $scope.removeItem = function (index) {
        $scope.items.splice(index, 1);
    };
})

不要注意&#34;聊天&#34; - 它是默认应用程序的功能。

此代码有效,我可以添加或删除项目,但这是具有空属性的项目。 $scope.itemAmount$scope.itemName始终为空。

我正在Ripple Emulator启动应用。

我做错了什么以及为什么新项目的属性是空的?

1 个答案:

答案 0 :(得分:4)

您与amountname绑定$scope.itemAmount$scope.itemName

$scope.items.push({
   amount: $scope.itemAmount,
   name: $scope.itemName
});

当您执行以下操作时,由于双向绑定,空字符串将存储到该项目中。

 $scope.itemAmount = "";
 $scope.itemName = "";

因此,您应该将这两个值从模板作为参数传递给函数addItem,如此

<button ng-click="addItem(itemAmount,itemName )">Add to list</button>

并在控制器中:

$scope.addItem = function (itemAmount, itemName) {
   $scope.items.push({
      amount: itemAmount,
      name: itemName
   });
   $scope.itemAmount = "";
   $scope.itemName = "";
};

完整代码演示:

var app = angular.module("myApp",[]);
app.controller("myController", function($scope){
  $scope.name="asd";
  $scope.items = [];
  $scope.addItem = function (itemAmount, itemName) {
   $scope.items.push({
      amount: itemAmount,
      name: itemName
   });
   $scope.itemAmount = "";
   $scope.itemName = "";
 };
  
 $scope.removeItem = function (index) {
   $scope.items.splice(index, 1);
 };
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp" ng-controller="myController">
  <div id="AddItem">
      <h3>Add Item</h3>
      <input value="1" type="number" placeholder="1" ng-model="itemAmount">
      <input value="" type="text" placeholder="Name of Item" ng-model="itemName">
      <br />
      <button ng-click="addItem(itemAmount,itemName)">Add to list</button>
    </div>
  <div id="UncheckedList">
      <h4>Unchecked:</h4>
      <table>
         <tr ng-repeat="item in items" class="item-unchecked">
            <td><b>amount:</b> {{item.amount}} -</td>
            <td><b>name:</b> {{item.name}} -</td>
            <td>
               <button ng-click="removeItem($index)">remove</button>
            </td>
          </tr>
      </table>
  </div>
</body>

编辑:在聊天中查看解决方案。在评论或here中找到要聊天的网址。