如何在AngularJS中的作用域之间附加输入框值

时间:2014-08-14 07:34:17

标签: javascript html angularjs

点击添加到列表后,我在这个示例中阅读了此示例http://jsfiddle.net/7n8NR/3/它将您输入的数据添加到未经检查:数组中检查项目按钮,当您按下检查项目按钮时,它会按已检查项目数组中的值。这是一个非常简单的例子。我要尝试的是我在未选中:数组中添加了新的输入文本框,并尝试将其值发送到已检查项数组,但它没有更新。我的工作演示在http://jsfiddle.net/7n8NR/240/

控制器:

// Add a Item to the list
$scope.addItem = function () {

    $scope.items.push({
        amount: $scope.itemAmount,
        name: $scope.itemName,
        //This one I have added
        qty: $scope.qty
    })

HTML:

     <table>
         <tr ng-repeat="item in items" class="item-unchecked">
            <td><b>amount:</b> {{item.amount}} -</td>
            <td><b>name:</b> {{item.name}} -</td>
             /*This is one I have added*/
            <td><b>qty:</b> <input type="number" ng-model="qty" /></td>
         </tr>
     </table>
        <button ng-click="toggleChecked($index)">check item</button>

我只是在尝试,但不知道这样做的正确方法。请任何人都可以提供帮助。提前致谢。

1 个答案:

答案 0 :(得分:1)

我刚刚对您的代码进行了一些更改(将qty添加到模型中)。现在它的工作。

但是它不起作用的主要原因是 - 因为这一行:

<button ng-click="toggleChecked($index)">check item</button>

在ng-repeat之外,toggleChecked($ index)没有得到索引值。

这是更新的脚本:http://jsfiddle.net/7n8NR/242/