【问题标题】:Modify model on change of input value inside ng-repeat修改 ng-repeat 中输入值变化的模型
【发布时间】:2016-01-07 21:57:37
【问题描述】:

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

【问题讨论】:

  • “当另一个改变时”是什么意思?
  • @ShaohaoLin 当输入 1 的值改变时,计算输入 2 的值
  • 您只需将$scope.bought_product.vat_price 更改为$scope.vm.calculateVatPrice(bought_product.price)
  • @JohannesJander 不知道你有没有注意到,它在一个ng-repeat里面,所以bought_product.vat_price不是一个简单的从控制器改变的值

标签: angularjs ng-repeat angular-ngmodel


【解决方案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;
}

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

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

【讨论】:

  • 请仔细阅读,bought_product.vat_price 位于ng-repeat 中,也就是说不能像你说的那样从控制器访问...
  • 哦,你是对的,我会纠正我的答案。如果可能的话,请在之后删除反对票:)
猜你喜欢
  • 1970-01-01
  • 2017-08-24
  • 1970-01-01
  • 2013-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多