2

In my Angular 1.4.8 app I want to calculate the difference between an original value and the value after it has been changed by the user.

<table>
  <tr>
    <td>Value</td>
    <td>Difference</td>
  </tr>
<tr ng-repeat="value in values">
  <td>{{ value }}</td>
  <td>{{ ::value - value }}</td>
<tr>
</table>

The values are an array, for example $scope.values = [1,5,8,3];.

But this doesn't work. Angular does the subtraction first and then binds the difference field forever at zero. Or if I do this, it just crashes:

<td>{{ (::value) - value }}</td>

1 Answer 1

3

The one-time binding operator isn't associative, it applies to the whole watch expression. What you can do is set a variable one-time and use that.

<tr ng-repeat="value in values">
  <td>{{:: originalValue = value }}</td>
  <td>{{originalValue - value }}</td>
<tr>

In this example the first watcher will compute originalValue until it stabilizes at which point the watcher will be de-registered, freezing both the DOM and the value of originalValue. The second watcher will continue computing its expression with the frozen value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.