根据 ng-repeat 中输入值的变化修改模型

Modify model on change of input value inside ng-repeat

我有一个 table,其中我列出了一些带有一些输入的产品,基本上我想要的是在另一个输入值发生变化时更改输入值,这里是 <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>

您将不得不更换您的

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;
}

当然你必须用你计算vat-price的实际业务逻辑替换它。

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