2

我在 VS2015 中创建了选项卡式离子应用程序。现在我想在那里添加简单的列表,可以添加/删除项目(类似于这个 - 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);
    };
})

不要关注“聊天”——这是默认应用程序的功能。

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

我正在Ripple Emulator中启动应用程序。

我在做什么错,为什么新项目的属性是空的?

4

1 回答 1

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>

编辑:在聊天中查看解决方案。在评论或此处查找 url 聊天。

于 2015-08-25T21:47:34.493 回答