Javascript 角度形式集变量相等表达式

Javascript 角度形式集变量相等表达式,javascript,angularjs,Javascript,Angularjs,我的目标如下: $scope.module.discount = [{ licences: 0, discountPercentage: 0, new_value_pr_licence: 0 }] 以及以下简单形式: <div class="form-group"> <label>Pris pr licens</label> <div c

我的目标如下:

  $scope.module.discount = [{
            licences: 0,
            discountPercentage: 0,
            new_value_pr_licence: 0
        }]
以及以下简单形式:

    <div class="form-group">
    <label>Pris pr licens</label>
    <div class="input-group m-b">
        <input type="number" class="form-control" ng-model="module.price_pr_licence">
        <span class="input-group-addon">Kr</span>
    </div>
</div>
<div class="col-xs-12">
    <table>
        <thead>
        <th>Licenser</th>
        <th>% rabat</th>
        <th>Ny pris pr stk</th>
        </thead>
        <tbody>
        <tr  ng-repeat="discount in module.discount">
            <td>
                <input class="form-control" ng-model="discount.licences" type="number" required="">
            </td>
            <td>
                <input class="form-control" type="number" ng-model="discount.discountPercentage" required="">
            </td>

            <td>
                <button class="btn btn-sm btn-default">{{module.price_pr_licence * (1-(discount.discountPercentage / 100))}}</button>
            </td>
        </tr>
        </tbody>
    </table>
</div>
现在我想提出:

discount.new_value_pr_licence = module.price_pr_licence * (1-(discount.discountPercentage / 100))
然而,我不太确定如何做到这一点


如何将变量绑定到这样的表达式?

您可以使用计算属性(通过访问器)来保持整洁,还可以节省angular不支持完整JS表达式树的表达式解析器

 $scope.module.discount = [{
            licences: 0,
            discountPercentage: 0,
            new_value_pr_licence: 0,
            get discountedPrice() {
                return ($scope.module.someValue + this.someOtherValue / 100)
            }
        }]
那么稍后您只需将其引用为

 {{discount.discountedPrice}}
小提琴:


何时设置?在按钮上单击?如果是这样的话,您可以这样做:ng click=“discount.new\u value\u pr\u license=module.price\u pr\u license*(1-(discount.discountPercentage/100))@BBauer42该按钮不需要单击(我知道这有点不清楚),但基本上只要表达式更改,就应该设置折扣值。谢谢您的回复。这会将变量设置为等于它还是只显示表达式的结果?我需要设置该值,因为我使用HTTPon变量将其发送到我的API。你是说字段?-它没有将任何字段/变量设置为anythig-它只是当场读取它们,但您会认为这是一个字段被设置为正确的值,所以不:)它没有将左值设置为右值OK。我真的不可能设置字段吗?在将列表发送到HTTP请求以设置每个字段之前,不必循环遍历列表?为此,我将使用如下原型:function Discount(){};assign(Discount.prototype,{get discountedPrice(){}})。。然后您只需实例化module.discounters=[新折扣(数据),新折扣(数据)]
 {{discount.discountedPrice}}