Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular form set variable equal expression

I have the following object:

  $scope.module.discount = [{
            licences: 0,
            discountPercentage: 0,
            new_value_pr_licence: 0
        }]

And the following simple form:

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

AS you can see the button's value is an expression:

{{module.price_pr_licence * (1-(discount.discountPercentage / 100))}}

Now i wish to set:

discount.new_value_pr_licence = module.price_pr_licence * (1-(discount.discountPercentage / 100))

However im not quite sure how to do that.

How do you bind a variable to such an expression?

like image 359
Marc Rasmussen Avatar asked Aug 14 '26 22:08

Marc Rasmussen


1 Answers

you can go with a computed property (via an accessor) to keep things clean and also spare angular's expression parser that does not support full JS expression trees..

 $scope.module.discount = [{
            licences: 0,
            discountPercentage: 0,
            new_value_pr_licence: 0,
            get discountedPrice() {
                return ($scope.module.someValue + this.someOtherValue / 100)
            }
        }]

Then later on you just reference this as

 {{discount.discountedPrice}}

Fiddle:

Fiddle

like image 145
Peter Aron Zentai Avatar answered Aug 17 '26 11:08

Peter Aron Zentai