修改ng-repeat内输入值变化的模型

时间:2016-01-07 21:57:37

标签: angularjs ng-repeat angular-ngmodel

我有一个表格,我列出了一些带有输入的产品,基本上我想要的是在另一个更改时更改输入值,这里是<tr>

<tr ng-repeat="bought_product in vm.bought_products track by bought_product.id">
    <td>
        {{ bought_product.name }}
    </td>
    <td>
        <input type="number" class="form-control"
               min="1" max="1000" placeholder="#"
               ng-model="bought_product.quantity">
    </td>
    <td>
        <div class="input-group">
            <span class="input-group-addon">$</span>
            <!-- This is the input where I'll insert a price -->
            <input type="number" class="form-control no-spin"
                   min="1" max="1000" placeholder="#" ng-change="vm.calculateVatPrice(bought_product.price)"
                   ng-model="bought_product.price">
        </div>
    </td>
    <td>
        <div class="input-group">
            <span class="input-group-addon">$</span>
            <!-- This input will automatically be filled -->
            <input type="number" class="form-control no-spin"
                   min="1" max="1000" placeholder="#"
                   ng-model="bought_product.vat_price">
        </div>
    </td>
</tr>

1 个答案:

答案 0 :(得分:1)

您必须更换

ng-change="vm.calculateVatPrice(bought_product.price)"

通过

ng-change="vm.calculateVatPrice(bought_product)"

在你的vm.calculateVatPrice函数中你必须像这样计算和设置vat_price

calculateVatPrice = function (product) {
    product.vat_price = product.price * 1.18;
}

当然,您必须用实际的业务逻辑来替换它来计算增值税价格。

所以诀窍是交出对产品对象的引用并相应地更新值。