James
James

Reputation: 111920

Echo series of computed values

Given:

<input ng-model="x.costA" value="100" step="any" type="number" />
<input ng-model="x.costB" value="100" step="any" type="number" />

I need to take x.costA and x.costB (whenever the user updates the values) and compute a series of results that will be outputted into a <div> somewhere else on the page: Abstractly:

<div>
    Result 1: {{ result1 }}
    Result 2: {{ result2 }}
    Result 3: {{ result3 }}
</div>

These three different results are computed using the input data, costA and costB.

The computations that need to occur are quite complex so I do not want to place the arithmetic within {{...}}. I would rather have it in a Angular Controller/Service (not sure which??).

What's the best way to achieve this?

Upvotes: 0

Views: 406

Answers (1)

Foo L
Foo L

Reputation: 11137

You can put a $watch on x.costA+x.costB so when they change, all your results are computed. However, since you mentioned the results are complex/expensive, you may want to add a button for the user to click to trigger the computation by the user.

<button ng-click="computeNow()">Go!</button>

In the controller:

$scope.computeNow() = function() {
  $scope.result1 = $scope.x.costA + $scope.x.costB;
  // etc.
}

Upvotes: 1

Related Questions